htaccess - 将所有流量重定向到除现有 301 之外的另一个域

htaccess - Redirecting all traffic to another domain except existing 301's

我的 htaccess 文件充满了 301 重定向,如下所示:

Redirect 301 /old-page.html https://www.example.com/new-page

大约有 100 个这样的重定向。我想做的是将所有流向旧站点的流量重定向到新站点,不包括现有的 301

因此,如果有人去 old-site.com/old-page.html,它将把他们带到 new-site.com/new-page,如果有人去 old-site.com/random-page.html,它将把他们带到 new-site.com - 只是主页.

是否可以在不重写当前 301 的情况下使用 mod_rewrite 和 mod_alias 执行此操作?

您需要在所有规则前使用 RewriteCond,如下所示:

RewriteCond %{HTTP_HOST} ^(?:www\.)domain\.com$ [NC]

如果您还希望处理以下所有规则,请不要在 RewriteCond 语句中使用 L(last)标志。

来源:Redirect all urls exactly, just change domain name

您可以保留所有 301 规则。只需将此通用 301 规则 插入现有规则 下方:

# all existing 301 rules go here

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?old-site\.com$ [NC]
RewriteRule ^ http://new-site.com/? [L,R=301]