Question

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]
Was it helpful?

Solution

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

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top