Forum Moderators: phranque

Message Too Old, No Replies

redirect non-www to www considering both HTTP and HTTPS

best syntax?

         

skyeflye

5:31 am on Jan 13, 2006 (gmt 0)

10+ Year Member



Hello,

Just like many others here, I would like to 301 redirect all requests for "example.com" to "www.example.com". But I have not seen any code samples (or even questions) about this issue which account for both the HTTP protocol and the HTTPS protocol.

Take the following code for example (the "jdMorgan standard") :)

# Setup
RewriteEngine on
# Redirect non-www to www
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,QSA,L]

The above code in the httpd.conf file will obviously send all "non-www" requests to the HTTP "www" version of the website.

It seems to me that this could be problematic if a user makes a request like:

https://example.com/checkout.cgi

...because (it seems) they would get redirected to:

http://www.example.com/checkout.cgi

Assuming the "checkout.cgi" script required SSL to run properly/securely, this redirect might cause a technical problem, yes?

My question is, how could I assure that the request protocol (HTTP/HTTPS) does not change during such a redirect? Am I just inventing a problem where there actually isn't one?

Thanks!

jdMorgan

6:51 am on Jan 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wouldn't tinker with the SSL side of things; It shouldn't be necessary unless you let robots crawl your SSL stuff.

You can prevent your HTTP domain redirect code from messing with the HTTPS side by checking the server port number:


# Setup
RewriteEngine on
# Redirect non-www to www
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]

Here, the rule is suppressed if the request connects on port 443, the standard SSL port. Tweak to suit.

Use of [QSA] is only needed if you are adding something to an existing query string. Otherwise, the existing query string is preserved by default.

Note for other readers: This code is intended for use in httpd.conf, not .htaccess. Delete the leading slash on the RewriteRule pattern for use in .htaccess.

Jim

skyeflye

7:14 am on Jan 13, 2006 (gmt 0)

10+ Year Member



Thank you very kindly for your (as always) well-informed post.

This is of great help to me, and hopefully will be for many others as well.

Cheers!

skyeflye

10:18 pm on Jan 20, 2006 (gmt 0)

10+ Year Member



Thanks again! Although I now have one additional question about the above Rewrite Conditions.

The sequence of Conditions and the final Rule seem to work really well except that it doesn't cause any redirect to happen when there is no file path requested after the domain name.

So for example:
http://example.com/index.html
...gets rewritten to:
http://www.example.com/index.html

...which is great!

However,
http://example.com/
...is not getting rewritten at all, and I'm not sure why. It sure looks like it should be getting rewritten, just based on first condition.

Should I try adding another condition line? Would something like the following work in all cases, including the index page?


# Setup
RewriteEngine on
# Redirect non-www to www
RewriteCond %{HTTP_HOST} ^example\.com [NC] ###added [NC] flag ###
RewriteCond %{HTTP_HOST} !^$ ###new line inserted here ###
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]

Or perhaps the example given in the Apache URL rewriting guide is even more "foolproof" (adapted version below):


# Setup
RewriteEngine on
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]

I am now most inclined to try this last example, directly above.

Again, thanks for any insight or experience anyone can share on this!