drmonkeyninja
drmonkeyninja

Reputation: 8540

ModRewrite canonical URLs in .htaccess file

I'm using ModRewrite to redirect URLs to their canonical ones in my .htaccess file. I've got something a bit like this:-

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

It works, but doesn't look pretty. Can I combine these conditions into a single rule?

Upvotes: 0

Views: 635

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

You can link the conditions together by including an OR in the brackets, since the Rule that they are tied to are all the same:

RewriteCond %{HTTP_HOST} ^www.ex\.co\.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.ex\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^ex\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Upvotes: 3

Related Questions