Reputation: 13080
I'm forcing a URL change if the user has accessed the site via http://mysite or http://www.mysite
However currently I only check that the site does not equal the main sub-domain which is http://store
I also have another sub-domain but this is being overwritten as well, so I need to change the way my current code works which is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^store\.mysite\.com [NC]
RewriteRule (.*) http://store.mysite.com/$1 [R=301,L]
I would prefer to have it check if the url is either of these:
http://mysite.com
http://www.mysite.com
...and then apply the rewrite rule, how do I change my Rewrite condition to do this?
Upvotes: 1
Views: 8100
Reputation: 165188
Try this one -- works just fine here:
RewriteCond %{HTTP_HOST} ^(mysite\.com|www\.mysite\.com)$ [NC]
RewriteRule .* http://store.mysite.com%{REQUEST_URI} [R=301,L]
QSA
flag here since it is 301 redirect (depends on your actual URLs) -- [R=301,QSA,L]
.Other approaches:
1) Separate condition for each domain name (need to specify that OR
logic needs to be used instead of default AND
):
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule .* http://store.mysite.com%{REQUEST_URI} [R=301,L]
2) The same as above .. but using plain text comparison instead of regex:
RewriteCond %{HTTP_HOST} =mysite.com [NC,OR]
RewriteCond %{HTTP_HOST} =www.mysite.com [NC]
RewriteRule .* http://store.mysite.com%{REQUEST_URI} [R=301,L]
Upvotes: 4
Reputation: 13080
So far I have done this:
RewriteCond %{HTTP_HOST} mysite\.com [NC]
RewriteCond %{HTTP_HOST} www\.mysite\.com [NC]
RewriteRule (.*) http://store.mysite.com/$1 [R=301,L]
Can any improvements be made upon this?
Actually the first RewriteCond isn't detecting http://mysite.com
Upvotes: 1