.htaccess - 在 .htaccess 中,重定向除一个之外的所有域

标签 .htaccess mod-rewrite redirect

我的服务器上有多个域。我想将它们全部重定向到一个(example.net)。

我的.htaccess:

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

我将服务器上的所有 URL 重定向到一个主域,但该域也在重定向到自身。所以 www.example.net 返回 301 Moved Permanently并重定向回自身。有人告诉我这对 SEO 不利。我怎么能解决这个问题?

最佳答案

你需要添加一个 Rewritecond 来防止它在你已经在你想要的域中时重定向。如果你用谷歌搜索,或者查看 RewriteCond section of Apache’s mod_rewrite documentation,网上有很多例子。 .

你正在寻找的是这样的:

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

关于.htaccess - 在 .htaccess 中,重定向除一个之外的所有域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11059301/

相关文章:

apache - 在 ErrorDocument 消息中设置字符集

javascript - 服务 worker : How to handle a 302 redirect response

javascript - 如何通过JavaScript重定向到404错误页面?

wordpress - 域名自动指向 HTTPS 而不是 HTTP 我没有 SSL 证书

apache - .htaccess 中不同协议(protocol)的 URL 重写

apache - 将 www 重定向到非 www 并删除 index.php

php - 将 .html 处理为 .php (lighttpd)?

apache - htaccess 重定向 URL 中的重复参数

apache - 如何才能mod_rewrite并保留查询字符串?

java - 从托管 bean 事件 JSF 重定向