Francis Kim
Francis Kim

Reputation: 4285

Valid apache mod_rewrite in .htaccess

I am currently making changes to the .htaccess file to mod_rewrite a few URLs. I've done some reading and came up with the following.

RewriteCond %{HTTP_HOST} ^(www\.)?foobar\.net [NC,OR]
RewriteCond %{HTTP_HOST} ^foobar\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^shop\.foobar\.com [NC]
RewriteRule ^(.*)$ http://www.foobar.com/$1 [R=301,NC,L]

So my question is, is the above sufficient to redirect the following domains to www.foobar.com, while maintaining the trailing URL (eg. www.foobar.net/booya should go to www.foobar.com/booya):

Upvotes: 0

Views: 48

Answers (1)

chris-l
chris-l

Reputation: 2841

Yes, and actually, if you are not serving other, independent domains but those ones, and your new foobar.com is on a different server, you could just remove those RewriteCond.

On the other hand, if you are serving in the same server the new domain, and you want that all other possible domains to be redirected to www.foobar.com (which I think it may be your case), you could try instead:

RewriteCond %{HTTP_HOST} !(^www\.foobar\.com) [NC]
RewriteRule ^(.*)$ http://www.foobar.com/$1 [R=301,NC,L]

That way, you don't have to worry that you may be forgetting some other domain to redirect.

Upvotes: 1

Related Questions