diggersworld
diggersworld

Reputation: 13080

Rewrite condition to check if not two URLs

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

Answers (2)

LazyOne
LazyOne

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]
  • Here I have placed 2 domain names in single condition using regex pattern.
  • I have also modified RewriteRule a bit (that's how I preferring doing it).
  • You may also want to add 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

diggersworld
diggersworld

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

Related Questions