使用 .htaccess 覆盖现有的 "noindex, nofollow" X-Robots-Tag header?

Using .htaccess to override existing "noindex, nofollow" X-Robots-Tag header?

我正在尝试设置 X-Robots-Tag 以允许 Googlebot 为我的网站编制索引。我没有 robots.txt 文件,我的任何 html 文件中也没有任何与 X-Robots-Tag 相关的元标记。 Apache 服务器返回 header,其中 X-Robots-Tag 设置为 "noindex, nofollow"。如何通过编辑 .htaccess 文件取消设置此标记?

这是我在使用 Chrome 插件“Robots Exclusion Checker”时得到的结果:

X-Robots status BLOCKED noindex,nofollow.

Date: Thu, 23 Jul 2020 20:27:46 GMT
Content-Type: text/html
Content-Length: 1272
Connection: keep-alive
Keep-Alive: timeout=30
Server: Apache/2
X-Robots-Tag: noindex, nofollow
Last-Modified: Fri, 09 Mar 2018 19:26:43 GMT
ETag: "ae0-xxxxxxxxxx-gzip"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip
Cache-Control: max-age=3600
Expires: Thu, 23 Jul 2020 21:27:46 GMT

我的 .htaccess 文件的内容:

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>

Header onsuccess unset X-Robots-Tag
Header always set X-Robots-Tag "index,follow"

我试过将其添加到 .htaccess 文件的底部:

<files *.html>
Header set X-Robots-Tag "index,follow"
</files>

然后我从 Chrome 分机得到这个回复:

X-Robots BLOCKED noindex,nofollow,index,follow.

(注意它在下面的列表中出现了两次。)

Date: Thu, 23 Jul 2020 20:39:42 GMT
Content-Type: text/html
Content-Length: 1272
Connection: keep-alive
Keep-Alive: timeout=30
Server: Apache/2
X-Robots-Tag: noindex, nofollow
Last-Modified: Fri, 09 Mar 2018 19:26:43 GMT
ETag: "ae0-xxxxxxxxxxxxx-gzip"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip
Cache-Control: max-age=3600
Expires: Thu, 23 Jul 2020 21:39:42 GMT
X-Robots-Tag: index,follow

有没有办法把原来的X-Robots-tagheader删掉换成新的?我尝试了 Header unset X-Robots-Tag,但没有成功(仍然显示“BLOCKED noindex,nofollow”)。


解决方案:对我有用的是包含一个 robots.txt 文件并确保所有超链接都以尾部斜杠结尾。似乎没有尾部斜杠我得到了 301 重定向,其中包括违规的 noindex,nofollow header.

My index.html page is very, very simple and only hyperlinks inside the body to other parts of the site.
The site is hosted on ...

如评论中所述,您首先应该确定设置此 header 的来源,而不是试图覆盖(或取消设置)它。这不是 Apache 默认做的事情,这个 header 必须在某处显式设置。

如果您没有设置此 header(在您的 server-side 脚本或文件系统路径上的任何 .htaccess 文件中 - 甚至在文档根目录之上),则必须在vHost/server 配置。如果您无权访问服务器配置,那么您应该联系您的虚拟主机以查看问题所在。

<files *.html>
Header set X-Robots-Tag "index,follow"
</files>

这通常会“有效”,除非之前已在响应 header 的 always table 上设置了 header。在这种情况下,您需要执行相同的操作。例如:

Header always set X-Robots-Tag "index,follow"

您不需要 <Files> 包装器 - 除非您特别想要针对仅映射到 *.html 文件的请求?我会想象“noindex,nofollow”header 被设置在 每个 请求(例如图像和其他静态资源)。

但是,您不需要明确设置“index,follow”——因为这是搜索引擎执行的默认行为,无论 header 是否设置。因此,在这种情况下,您只需要 unset header (正如您也建议的那样),但是同样,您需要使用 always [= headers 的 60=](如果那是 header 开始设置的 table)。例如:

Header always unset X-Robots-Tag

“总是”table 的命名可能有点误导,因为上面看起来(对于不经意的 reader)header 可能是 总是 未设置(与 有时 相反)- 但事实并非如此。有两个单独的 groups/tables 响应 header:“always”和“onsuccess”(默认)。两者是相互排斥的。不同之处在于“总是”组 总是 应用 - 即使在错误和内部 rewrites/subrequests 时也是如此。默认组不是。

参考:
https://httpd.apache.org/docs/2.4/mod/mod_headers.html#header