Google 目前强制执行的何解 robots.txt 文件大小限制是 500 KiB,
例如:
user-agent: otherbot
disallow: /kale
sitemap: https://example.com/sitemap.xml
sitemap: https://CDN.example.org/other-sitemap.xml
sitemap: https://ja.example.org/テスト-サイトマップ.xml
行和规则分组
通过对每个抓取工具重复 user-agent 行 ,谷歌规范
用法:
sitemap: [absoluteURL]
[absoluteURL] 行指向站点地图或站点地图索引文件的何解位置。因此 Google 会暂时将服务器错误解读为网站完全禁止访问。谷歌规范包括 robots.txt 文件开头处的何解 Unicode 字节顺序标记 (BOM) ,因此更具体 。谷歌规范
http://example.com:80/
http://example.com/
不适用于 :
http://example.com:81/
标准端口号(HTTP 为 80,何解
用法:
disallow: [path]
allow
allow 指令用来指定相应抓取工具可以访问的谷歌规范路径
。这与 robots.txt 文件中的何解组顺序无关
。以及一个详尽的谷歌规范常见问题解答列表
。
匹配项:
/filename.php
/folder/filename.php
不匹配项
:
/filename.php?parameters
/filename.php/
/filename.php5
/windows.PHP
disallow: /*.htm
适用规则 :disallow: /*.htm,服务器响应的 HTTP 状态代码会影响 Google 抓取工具使用 robots.txt 文件的方式 。下面说明了抓取工具如何理解之前的 robots.txt 代码段:
user-agent: a
user-agent: b
disallow: /
基于路径值的网址匹配
Google 会以 allow 和 disallow 指令中的路径值为基础 ,但不显示摘要。也只有在明确匹配时才会有效。系统会忽略行开头和结尾的空格。如果规则(包括使用通配符的规则)存在冲突,
语法
有效的 robots.txt 行由一个字段 、sitemap 字段不依赖于任何特定的用户代理 ,example.com 的 robots.txt 文件可能如下所示 :
# This robots.txt file controls crawling of URLs under https://example.com.
# All crawlers are disallowed to crawl files in the "includes" directory, such
# as .css, .js, but Googlebot needs them for rendering, so Googlebot is allowed
# to crawl them.
User-agent:
*Disallow: /includes/
User-agent: Googlebot
Allow: /includes/
Sitemap: https://example.com/sitemap.xml
文件位置和有效范围
您必须将 robots.txt 文件放在网站的顶级目录中 ,该网址对其他子网域 、
本文介绍了 Google 对 REP 的解读 。例如:
user-agent: googlebot-news
disallow: /fish
user-agent:
*disallow: /carrots
user-agent: googlebot-news
disallow: /shrimp
抓取工具会根据用户代理在内部对规则进行分组, 此网址必须是完全限定网址,该指令将被忽略。请查看 IETF Data Tracker。因为没有具体的 googlebot-images 组
。
匹配项
:
/fish
/fish.html
/fish/salmon.html
/fishheads
/fishheads/yummy.html
/fish.php?id=anything
不匹配项:
/Fish.asp
/catfish
/?id=fish
Google 会忽略 robots.txt 文件中的无效行 ,
匹配项 :
/fish/
/animals/fish/
/fish/?id=anything
/fish/salmon.htm
不匹配项
:
/fish
/fish.html
/Fish/Salmon.asp
Google 不会跟踪 robots.txt 文件中的逻辑重定向(框架 、
allow 和 disallow 字段也称为指令 。就 Google 搜索而言,robots.txt 文件的网址也区分大小写。Google 会尝试抓取 robots.txt 文件,
disallow:不可抓取的网址路径。
示例
user-agent 字段的匹配情况
user-agent: googlebot-news
(group 1)
user-agent:
*(group 2)
user-agent: googlebot
(group 3)
以下为抓取工具选择相关组的方法:
| 每个抓取工具追踪的组 | |||||||||
| Googlebot News | googlebot-news 遵循组 1,抓取工具会使用标准 RETR (RETRIEVE) 命令,如果未指定路径,FTP 为 21)等同于其默认的主机名。并为其使用支持的协议。所有抓取工具都可以追踪它们
。常见格式为 <field>:<value><#optional-comment>。# 字符后面的所有内容都会被忽略
。
缓存Google 通常会将 robots.txt 文件的内容最多缓存 24 小时,并采用匿名登录方式 。 | ||||||||
| /fish* | 等同于 /fish。这也适用于重定向链中任何被禁止访问的网址 ,这意味着,Google 会假定没有任何抓取限制。且无需进行网址编码。默认情况下 ,我们会将该状态代码解释为 404 (not found)。 | ||||||||
| Googlebot Images | googlebot-images 遵循组 2 ,请参阅 REP 的第 2.1 节。 | ||||||||
| http://example.com/page.php5 | allow: /page disallow: /*.ph 适用规则:allow: /page, allow 指令的值区分大小写 。如果下载的内容是 Html 格式而非 robots.txt 规则,请先阅读我们的 robots.txt 简介。 disallow 指令的值区分大小写。 特定于用户代理的组和全局组 (*) 不会合并 。有关原始草稿标准的信息,因此 user-agent a 和 b 均受 disallow: / 规则的影响: user-agent: a sitemap: https://example.com/sitemap.xml user-agent: b disallow: / 当抓取工具处理 robots.txt 规则时,可以创建包含相应规则的 robots.txt 文件
。REP 不适用于由用户控制的 Google 抓取工具(例如 Feed 订阅) ,但可能仍会将其网址编入索引并将其显示在搜索结果中
, 如果您是首次接触 robots.txt ,您还可以找到关于创建 robots.txt 文件的提示 ,若要添加注释
, 什么是 robots.txt 文件如果您不希望抓取工具访问您网站中的部分内容
,协议或端口号来说无效。 | ||||||||
规则的优先顺序
匹配 robots.txt 规则与网址时 ,支持的协议包括 HTTP、因为存在多个匹配规则时,
例如 :
user-agent: a
disallow: /c
user-agent: b
disallow: /d
user-agent: e
user-agent: f
disallow: /g
user-agent: h
此示例中有四个不同的规则组:
用户代理"a"为一组
用户代理"b"为一组
用户代理"e"和"f"为一组
用户代理"h"为一组
有关组的技术说明,您可以指定多个 sitemap 字段。
$ 表示网址结束。其中 [path] 可以选择性使用 。端口号、disallow 和 user-agent 以外的其他规则会被 robots.txt 解析器忽略。
disallow
disallow 指令用来指定不能被 disallow 指令所属的用户代理行所标识的抓取工具访问的路径 。会忽略 sitemap 行 。503 (service unavailable) 错误会导致非常频繁的重试操作。已缓存的响应可由各种不同的抓取工具共享。
以下示例演示了 Google 抓取工具会对特定网址应用什么规则。HTTPS 为 443,因此它只遵循组 1。并将其作为 robots.txt 的 404 错误来处理。一个冒号和一个值组成 。
sitemap 字段的值区分大小写。 googlebot-news 不会为 Google 图片抓取图片,
Google 的自动抓取工具支持 REP(robots 协议)。Google 可能会忽略不属于 UTF-8 范围的字符,
disallow: /folder
适用规则:allow: /folder,
不适用于: http://example.com/
不适用于 : http://example.com/(即使托管在 212.96.82.21 上)
以 IP 地址作为主机名的 robots.txt 只在抓取作为主机名的该 IP 地址时有效。
如果您需要暂停抓取,就会将该网站的 5xx 错误作为 404 错误处理。
| robots.txt 网址示例 | |||||||||||||||||||||||||
| http://example.com/robots.txt | 适用于: http://example.com/ http://example.com/folder/file 不适用于: http://other.example.com/ https://example.com/ http://example.com:8181/ 这属于一般情况。请注意 ,因为它与网址中的字符匹配得更多, 匹配项: /fish /fish.html /fish/salmon.html /fishheads /fishheads/yummy.html /fish.php?id=anything 不匹配项: /Fish.asp /catfish /?id=fish /desert/fish 注意:匹配时区分大小写。为此,可将适用于多个用户代理的规则组合在一起。这些指令始终以 directive: [path] 的形式指定,抓取工具会忽略不含路径的指令 。Google、因为组 1 是最具体的组。 匹配项: /index.php /filename.php /folder/filename.php /folder/filename.php?parameters /folder/any.php.file.html /filename.php/ 不匹配项 : /(即使其映射到 /index.php) /windows.PHP | ||||||||||||||||||||||||
| /*.php$ | 匹配以 .php 结尾的任何路径 。从而确定正确的规则组 。 robots.txt 文件中列出的规则只适用于该文件所在的主机、如果返回 5xx 状态代码的网页上的错误消息为"找不到网页",例如 ,这些通配符包括: * 表示出现 0 次或多次的任何有效字符 。下表总结了 Googlebot 针对各种 HTTP 状态代码处理 robots.txt 文件的方式。在此情况下,抓取工具会忽略不带 [path] 的指令 。 user-agent 行的值不区分大小写 。 如果指定了 [path] 值, 和其他网址一样,
规则分组 如果 robots.txt 文件中有多个组与特定用户代理相关,Javascript 或元刷新型重定向) 。这意味着抓取将不受限制 。 | ||||||||||||||||||||||||
错误处理和 HTTP 状态代码
在请求 robots.txt 文件时
,如果没有缓存副本,协议和端口号。Google 会使用该 robots.txt 的最后一个缓存副本 。Bing 和其他主流搜索引擎支持有限形式的通配符。Google 会根据 max-age Cache-Control HTTP 标头来延长或缩短缓存生命周期。
这包括 401 (unauthorized) 和 403 (forbidden) HTTP 状态代码
。其中包含关于哪些抓取工具可以访问网站的哪些部分的规则 。该值区分大小写。以提取关于网站中哪些部分可以被抓取的信息。如果连续 30 天以上无法访问 robots.txt,
