views:

34

answers:

3

I have two websites that is actually the same where example.com shares all files from examples.com. So whatever changes made in exampples.com, example.com automatically gets updated. That means they have the same .htaccess file. The problem is, I want to both sites redirects to non www to a www url. I got this:

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

replace example.com with %{HTTP_HOST} to make your rules host independent

mathroc
Even in the `RewriteCond` line?
Ignacio Vazquez-Abrams
no, you're right just in the rewrite rule
mathroc
+1  A: 
RewriteCond %{HTTP_HOST} ^[^\.]+\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Ignacio Vazquez-Abrams
+1  A: 

This should do it:

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

Basically you're adding an OR condition to say if either example.com or examples.com doesn't begin with www. then add it to the respective domain name.

Dolbz
+1 this one works, but I like Ignacio's answer.
jun
cool. In case you ever add more domains that you don't want included in the rule then you'll need to use a rule like this which explicitly states all the domains to rewrite.
Dolbz
Just realised. Two RewriteCond's aren't required. you could have: RewriteCond %{HTTP_HOST} ^examples?\.com$ [NC]
Dolbz