问题描述
ReadTheDocs自动为项目生成 <dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.31</version>
</dependency>
和robots.txt
。每次我部署项目的新次要版本(例如sitemap.xml
)时,我都会隐藏以前的次要版本(例如4.1.10
)。 ReadTheDocs将所有版本的条目添加到4.1.9
,但是隐藏版本也添加到sitemap.xml
。结果是,由于以前的站点地图条目现在已被新生成的robots.txt
阻止,因此,提交到Google Search Console的站点地图此时会导致“提交的网址被robots.txt阻止”错误。
ReadTheDocs为每个版本生成一个站点地图URL,因此对于robots.txt
,我们有一个这样的条目,例如:
4.1.9
当发布<url>
<loc>https://pyngrok.readthedocs.io/en/4.1.9/</loc>
<lastmod>2020-08-12T18:57:47.140663+00:00</lastmod>
<changefreq>monthly</changefreq>
<priority>0.7</priority>
</url>
并隐藏先前的次要版本时,新生成的4.1.10
将得到:
robots.txt
我相信此disallow: /en/4.1.9/ # Hidden version
会导致Google搜寻器引发错误。
实际上,在disallow
中,我想要的只是sitemap.xml
,latest
和develop
,我不太在乎每个版本的爬网。但是我只能配置as I understand it from ReadTheDocs docs,是静态的stable
。
我想要发布的是自己的静态robots.txt
,而不是使用自动生成的静态sitemap.xml
。有什么办法做到这一点?
解决方法
在研究了一些想法之后,这是我想到的另一个解决方案。由于这个问题经常被问到并且经常被作为针对GitHub上的ReadTheDocs的错误而打开(不是,它似乎没有得到很好的支持和/或记录),因此,我将在这里分享我的解决方法,以供其他人查找。
如上所述和文档中所述,尽管ReadTheDocs允许您覆盖自动生成的robots.txt
并发布自己的文档,但sitemap.xml
却不能。不清楚为什么。无论如何,您都可以发布一个不同 sitemap.xml
,我将其命名为sitemap-index.xml
,然后告诉robots.txt
指向您的自定义站点地图。
对于我的自定义sitemap-index.xml
,我只放置我关心的页面,而不是生成的版本(因为stable
和latest
确实是我希望搜索引擎抓取的,而不是版本页面):
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>https://pyngrok.readthedocs.io/en/stable/</loc>
<changefreq>weekly</changefreq>
<priority>1</priority>
</url>
<url>
<loc>https://pyngrok.readthedocs.io/en/latest/</loc>
<changefreq>daily</changefreq>
<priority>0.9</priority>
</url>
<url>
<loc>https://pyngrok.readthedocs.io/en/develop/</loc>
<changefreq>monthly</changefreq>
<priority>0.1</priority>
</url>
</urlset>
我创建了自己的robots.txt
,该命令告诉Google除了主要分支 指向我的自定义sitemap-index.xml
以外,不要抓取其他任何内容。
User-agent: *
Disallow: /
Allow: /en/stable
Allow: /en/latest
Allow: /en/develop
Sitemap: https://pyngrok.readthedocs.io/en/latest/sitemap-index.xml
我将这两个文件放在/docs/_html
下,并在我的Sphinx conf.py
文件(位于/docs
中)中添加:
html_extra_path = ["_html"]
这里也显示在仓库for reference中。
ReadTheDocs重建必要的分支后,将/en/latest/sitemap-index.xml
赋予Google Search Console而不是默认分支,请Google重新处理您的robots.txt
,不仅将解决爬网错误,而且Google将正确索引隐藏先前次要版本的网站。