关于.htaccess:301重定向到除robots.txt外的所有内容

301 redirects to all except robots.txt

我们已将网站移至新域,并希望将旧网站的所有页面从搜索引擎中删除。 它是相同的站点,相同的内容,只是一个新域,因此搜索引擎由于内容重复(可能)而花费时间。 我们已经将.htaccess 301从旧站点添加到新站点,如下所示:

1
redirect 301 / http://new-domain.com/

现在,要从搜索引擎中删除旧站点,我们将旧站点上的robots.txt更改为:

1
2
User-agent: *
Disallow: /

问题是,由于.htaccess 301重定向,搜索引擎正在从new-domain.com获取robots.txt。

如何限制robots.txt的301重定向?


删除Redirect指令,然后尝试使用此mod_rewrite规则:

1
2
RewriteEngine on
RewriteRule !^robots\\.txt$ http://other.example.com%{REQUEST_URI} [L,R=301]

这将重定向/robots.txt以外的任何请求。


我尝试了不同的配置并花了几个小时,终于找到了运行良好的代码,并希望获得期望的结果
这是禁止搜索引擎的代码。

1
2
User-agent: *
Disallow: /