Redirect non-www domain to www, preserving staging
I have two domains, www.example.com and staging.example.com (both hosted on the same box).
I'd like to always redirect example.com to www.example.com, but when I use solutions from other SO threads it breaks the staging domain.
Can someone point me in the right direction?
Thanks!
UPDATE: Does this cover htt开发者_如何学Pythonps as well? If not, what do I have to do to get that working?
You need this in your .htaccess file at the root of your directory :
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
the magic lies in the ^ which indicates that it should only rewrite the url if the adress STARTS by example.com. This leaves staging.example.com out of the loop.
In case you need to do that for HTTPS as well just add the following line to the conditions and tweak it for your need (=on or !=on) :
RewriteCond ${HTTPS} =on
Which gives you :
RewriteCond ${HTTPS} =on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Oh and don't forget to put the RewriteEngine on line at the top of your .htaccess file
RewriteEngine On
# not HTTPS
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# HTTPS
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
There should not be any conflict with staging.example.com
精彩评论