In .htaccess, redirect all domains except one

2020-03-01 07:13发布

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?

2条回答
爷、活的狠高调
2楼-- · 2020-03-01 07:41

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]
查看更多
Juvenile、少年°
3楼-- · 2020-03-01 08:01

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]
查看更多
登录 后发表回答