Forum Moderators: phranque
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!
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]
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
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!