single page + non-www to www + all to https not working

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 10
Joined: 29 Jul 2012, 05:50

single page + non-www to www + all to https not working

29 Jul 2012, 06:00

Hello.

I put together the following code from examples to do the following.

1. Redirect /default.htm requests to root (/)

2. Redirect any non-www to www request. mysite.com should be redirected to www.mysite.com

3. any non https should be redirected to https.

All this has to work while redirecting the client ONLY ONCE thu a 301. Example: a 301 response to go to www and then another 301 response to go to https is not acceptable.


RewriteEngine on

#redirect old default page
RewriteRule default.htm / [R=301]

#non www to www
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^www\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://%2%3 [R=301]

#Redirect non-HTTPS to HTTPS
RewriteCond %{HTTP:Host} (.*)
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} (.*)
RewriteRule .? https://%1%2 [R=301,L]


Right now it is NOT working correctly. It is actually doing the opposite and redirecting WWW URLs to NON WWW. Also, it is doing 301 2 times, 1 for www to non www and then another one for non https to https

Server has v3 installed.

Is the above code correct ? I am very new to URL rewrite. Any help appreciated.

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: single page + non-www to www + all to https not working

30 Jul 2012, 06:44

In our examples section there's an example "Redirecting non-www version to www"(which is completely different from what you had):

Code: Select all
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]


In case you want to combine 2 rule and have 1 redirect instead of 2... than you need 1 rule as in the following:

Code: Select all
#non www to www
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? https://%1%2 [NC,R=301,L]


I'd also fix the root rule as in the following:

RewriteRule ^default\.htm$ / [NC,R=301,L]

Regards
Andrew

User avatar
Posts: 10
Joined: 29 Jul 2012, 05:50

Re: single page + non-www to www + all to https not working

30 Jul 2012, 10:39

Thanks Andrew!

I will forward this to the hosting company so they can implement it and will get back to you.

User avatar
Posts: 10
Joined: 29 Jul 2012, 05:50

Re: single page + non-www to www + all to https not working

30 Jul 2012, 17:09

Hi Andrew!

okay... the hosting admin uploaded the script to the .htaccess file in the root, but this script only redirects to https when there is no WWW in the url ...it DOES NOT redirect to WWW. in fact when i go to http://www.mysite.com it doesnt even redirect to SSL.




#non-www to www + non-ssl to ssl combined
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? https://%1%2 [NC,R=301,L]

#redirect old default page
RewriteRule ^default\.htm$ / [NC,R=301,L]

Is there something missing in the code ?

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: single page + non-www to www + all to https not working

31 Jul 2012, 04:08

It is missing, sorry. Try using the following:

Code: Select all
#non-www to www + non-ssl to ssl combined
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? https://www.%1%2 [NC,R=301,L]


In case you use 2 rules and have 2 redirects going, indeed you cover more requests(e.g. all http -->https and all non-www-->www)... in case you want it to be only one rule, than there're some flaws like with www.domain.com/, which doesn't match all conditions. This rule redirects BOTH non-www and non-https.

Why are you so concerned about 2 redirects?

Regards
Andrew

User avatar
Posts: 10
Joined: 29 Jul 2012, 05:50

Re: single page + non-www to www + all to https not working

31 Jul 2012, 11:42

Andrew.

The reason i'm concerned about 2 redirects is search engines. Wouldn't search engines penalize rank if they request 1 page and get 2 redirects ?

Oh btw... I messed around with the old script and i'm only getting 1 redirect.

Here is what I have now that seems to work good:

RewriteRule ^default\.htm$ https://www.mysite.com/ [NC,R=301,L]


RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? https://www.%2%3 [NC,R=301,L]

RewriteCond %{HTTP:Host} (.*)
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} (.*)
RewriteRule .? https://%1%2 [NC,R=301,L]


correction: i just checked... yea... it doesnt work that good when sombody types in HTTP in capitals... is there a way to convert all to lower case before processing ? or even better.... treat uppercase and lowercase the same ? I will also try your rule and report how that one works.

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: single page + non-www to www + all to https not working

31 Jul 2012, 12:33

The request will be processed before it even reaches IIS, basically you'll have request going in ISAPI_Rewrite, processing, redirecting to www for example, and than the request has to go through ISAPI_Rewrite again as a new request... so only when the page is being loaded, only then the 301-redirect status will be received. But this needs to be double-checked.

Regards
Andrew

User avatar
Posts: 10
Joined: 29 Jul 2012, 05:50

Re: single page + non-www to www + all to https not working

31 Jul 2012, 14:38

I can confirm by using http://www.internetmarketingninjas.com/header-checker/ that before the changes i did to RewriteRule .? https://www.%2%3 [NC,R=301,L] the client was getting two 301 requests. First one for www and then a second one for https.

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: single page + non-www to www + all to https not working

01 Aug 2012, 06:57

LEts try to make it this way:

Code: Select all
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? https://www.%2%3 [NC,R,L]

RewriteCond %{HTTP:Host} (.*)
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} (.*)
RewriteRule .? https://%1%2 [NC,R=301,L]


So the first one will give 302 and the second one will give 301, if that acceptable for you.

OR

we use a compination provided + we'll cover the http://www.domain.com in a separate rule.

Which one is better?

Regards
Andrew

User avatar
Posts: 10
Joined: 29 Jul 2012, 05:50

Re: single page + non-www to www + all to https not working

01 Aug 2012, 13:45

"we use a compination provided"

Sorry you lost me here lol Anyway i think 301 is the only way to go SEO wise. somebody correct me if i'm wrong, since in fact this redirect IS Permanent meaning there wont be anything other than https://www.mysite.com

Did you look at the rule that I provided/modified ? Do you see there anything being wrong with it / conditions that wont match besides the capital HTTP condition ? I think all browsers convert http to lowercase or something anyway before transmitting again I might be wrong.

But other than that the rule that i modified is working fine as far as i'm concerned.

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: single page + non-www to www + all to https not working

02 Aug 2012, 05:22

I did some testing last might and I think we can go with:

Code: Select all
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? https://www.%2%3 [NC,R=301]

RewriteCond %{HTTP:Host} (.*)
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} (.*)
RewriteRule .? https://%1%2 [NC,R=301,L]


So the idea is to eliminate L flag from the first rule so it won't fire it without matching the second rule. that would be perfect for you

Regards
Andrew

User avatar
Posts: 10
Joined: 29 Jul 2012, 05:50

Re: single page + non-www to www + all to https not working

02 Aug 2012, 13:49

Is that going so solve the HTTP in capital to break the code ?

Because I currently have this code and it works fine (only 1 redirect is performed)

RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? https://www.%2%3 [NC,R=301,L]

RewriteCond %{HTTP:Host} (.*)
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} (.*)
RewriteRule .? https://%1%2 [NC,R=301,L]

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: single page + non-www to www + all to https not working

03 Aug 2012, 07:45

This won't fix http issue. Actually http issue is pretty weird.
Can you provide rewrite.log for the testing request? Logging issues described in FAQ

Regards
Andrew

User avatar
Posts: 10
Joined: 29 Jul 2012, 05:50

Re: single page + non-www to www + all to https not working

03 Aug 2012, 19:14

I will try to get a hold of the logs. But could you describe the difference between your code and the one I linked ? the only difference is the L parameter but how come both perform only 1 redirect ? I will implement your version of the code and will let you know if i see a difference.

User avatar
Posts: 10
Joined: 29 Jul 2012, 05:50

Re: single page + non-www to www + all to https not working

04 Aug 2012, 16:56

I implemented your version of the script, and it doesn't seem to make a difference, at least on the client side, but working well.

Thanks for all the help!

But now i have another problem:

https://www.mysite.com/?blah=blahhh&2blah=123blah now gets indexed in google search. that and any dynamic url for homepage points to the same content as https://www.mysite.com/

Here is the rule I wrote for that it and seems to work:

RewriteCond %{QUERY_STRING} .
RewriteRule ^(/)?$ %{REQUEST_URI}? [NC,R=301]

User avatar
Posts: 10
Joined: 29 Jul 2012, 05:50

Re: single page + non-www to www + all to https not working

05 Aug 2012, 15:28

Okay.

To be honest, I don't see the point to the L parameter anymore...

All my rules don't have an L attached to them except the last one (http to https)

Yet..
RewriteCond %{QUERY_STRING} .
RewriteRule ^(/)?$ %{REQUEST_URI}? [NC,R=301]

Performs 2 redirects ... lets review.

Original request: http://mysite.com/?iframe=true

First 301: http://mysite.com/

Second 301: https://www.mysite.com/ (at least i got www and https going at the same time, by hardcoding https://www.)


Is this a limitation of the software ? Why cant the software process all the rules before executing one final redirect ?

Here is my complete script:

Code: Select all

# Helicon ISAPI_Rewrite configuration file
# Version 3.1.0.82

RewriteEngine on

#redirect old default page
RewriteRule ^default\.htm$ https://www.mysite.com/ [NC,R=301]

#redirect non existing folder to homepage
RewriteRule ^community/ https://www.mysite.com/? [NC,R=301]

#redirect non existing file to homepage
RewriteRule ^pdf/junk\.pdf$ https://www.mysite.com/ [NC,R=301]
RewriteRule ^junk_service\.asp https://www.mysite.com/? [NC,R=301]

#redirect one specific page to another, preserving query strings
RewriteRule ^junk_pricing\.asp pricing.asp [NC,R=301]

#if there is a query passed to homepage, remove it
RewriteCond %{QUERY_STRING} .
RewriteRule ^(/)?$ %{REQUEST_URI}? [NC,R=301]

#Redirect non-WWW to WWW
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? https://www.%2%3 [NC,R=301]

#Redirect non-HTTPS to HTTPS
RewriteCond %{HTTP:Host} (.*)
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} (.*)
RewriteRule .? https://%1%2 [NC,R=301,L]




I could hardcode that query strings rule, but the point is not making it work, but figuring out why ISAPI_REWRITE executes the redirect before going thru all rewrite rules.

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: single page + non-www to www + all to https not working

06 Aug 2012, 05:55

Usually our customers simply want a rule that works. I can see you're trying hard to really get into it. I don't know why I haven't done it before...
ANywat, here's a piece of informatioon that covers all your questions: 1- "Exploding myths. Part1" and 2 - "Exploding myths. PArt2"

Regards
Andrew

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 10 guests