In .htaccess, redirect all domains except one

谁说胖子不能爱 提交于 2020-01-10 23:01:34

问题


I have multiple domains on my server. I want to redirect all of them to one (example.net).

My .htaccess:

RewriteEngine on 
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]

I’m redirecting all URLs on my server to one main domain, but that domain is also redirecting to itself. So www.example.net returns 301 Moved Permanently and redirects back to itself. I’m told that this isn’t good for SEO. How could I fix this?


回答1:


You need to add a Rewritecond to prevent it from redirecting when you’re already on the domain that you want. There are loads of examples online if you google it, or see the RewriteCond section of Apache’s mod_rewrite documentation.

What you’re looking for is something like:

RewriteEngine on 
Rewritecond %{HTTP_HOST} !^www\.example\.net
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]



回答2:


Just small note: Thanks goes to TRiG, but I had to remove one slash to make it work correctly (coz it added two slashes after domain name). This works for me:

RewriteEngine on 
Rewritecond %{HTTP_HOST} !^www\.example\.net
RewriteRule ^(.*)$ http://www.example.net$1 [R=301,L]


来源:https://stackoverflow.com/questions/11059301/in-htaccess-redirect-all-domains-except-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!