开发者

.htaccess redirection - two sharing websites

开发者 https://www.devze.com 2022-12-21 09:12 出处:网络
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 h

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 开发者_StackOverflow中文版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]


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号