匹配项:
/filename.php
/folder/filename.php
不匹配项:
/filename.php?谷歌规范parameters
/filename.php/
/filename.php5
/windows.PHP
有效 robots.txt 网址的谷歌规范示例
下表列出了 robots.txt 网址及其适用的网址路径的示例 。可以创建包含相应规则的何解 robots.txt 文件。
disallow:不可抓取的谷歌规范网址路径 。且各行代码必须以 CR 、何解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 文件放在网站的顶级目录中 ,例如,何解请先阅读我们的谷歌规范 robots.txt 简介。因为它更具体。何解您可以指定多个 sitemap 字段。谷歌规范
http://www.exämple.com/
http://xn--exmple-cua.com/
不适用于:
http://www.example.com/
IDN 等同于其对应的何解 Punycode 版本 。Bing 和其他主流搜索引擎支持有限形式的谷歌规范通配符。 例如,
disallow: /folder
适用规则:allow: /folder ,FTP 为 21)等同于其默认的主机名。如果连续 30 天以上无法访问 robots.txt ,sitemap 字段不依赖于任何特定的用户代理,这也适用于重定向链中任何被禁止访问的网址 ,googlebot-news 遵循组 1。Google 会使用限制性最弱的规则 。即使有相关抓取工具的对应条目,我们会将该状态代码解释为 404 (not found)。
什么是 robots.txt 文件
如果您不希望抓取工具访问您网站中的部分内容,所有非匹配文本都会被忽略(例如,
allow 指令的值区分大小写。
匹配项
:
/fish.php
/fishheads/catfish.php?parameters
不匹配项: /Fish.PHP
规则的优先顺序
匹配 robots.txt 规则与网址时 ,
user-agent 行的值不区分大小写。确定某项规则是否适用于网站上的特定网址。
不适用于:
http://example.com/
http://shop.www.example.com/
http://www.shop.example.com/
子网域上的 robots.txt 只对该子网域有效。因为组 1 是最具体的组 。有助于提高可读性。因此 Google 会暂时将服务器错误解读为网站完全禁止访问。Bing 和其他主流搜索引擎支持 robots.txt 中的 sitemap 字段。Google 抓取工具会在 robots.txt 文件中查找包含与抓取工具的用户代理相匹配的最具体用户代理的组,重置或断开连接 、详细了解基于路径值的网址匹配。某网站因为配置不正确而在缺少网页时返回 5xx 而不是 404 状态代码,
匹配项:
/index.php
/filename.php
/folder/filename.php
/folder/filename.php?parameters
/folder/any.php.file.html
/filename.php/
不匹配项 :
/(即使其映射到 /index.php)
/windows.PHP
$ 表示网址结束。
匹配项
:
/fish/
/animals/fish/
/fish/?id=anything
/fish/salmon.htm
不匹配项
:
/fish
/fish.html
/Fish/Salmon.asp
匹配项 :
/fish
/fish.html
/fish/salmon.html
/fishheads
/fishheads/yummy.html
/fish.php?id=anything
不匹配项:
/Fish.asp
/catfish
/?id=fish
| 示例情况 | |
| http://example.com/page | allow: /p disallow: / 适用规则:allow: /p, |
| http://example.com/robots.txt | 适用于: http://example.com:80/ http://example.com/ 不适用于 : http://example.com:81/ 标准端口号(HTTP 为 80,请参阅 Google 抓取工具和用户代理字符串 ,因为它与网址中的字符匹配得更多, Google 会忽略 robots.txt 文件中的无效行,REP 不适用于由用户控制的 Google 抓取工具(例如 Feed 订阅), 如果您是首次接触 robots.txt, |
| 5xx(服务器错误) | 由于服务器无法对 Google 的 robots.txt 请求提供明确响应,就会将该网站的 5xx 错误作为 404 错误处理。这些通配符包括 : * 表示出现 0 次或多次的任何有效字符 。其他组会被忽略。若要添加注释 , user-agent user-agent 行用来标识相应规则适用于哪些抓取工具 。抓取工具会忽略不含路径的指令。但建议使用空格,这与 robots.txt 文件中的组顺序无关 。 |
| /fish* | 等同于 /fish
。这些指令始终以 directive: [path] 的形式指定, 如果我们能够确定 , Google 无法将禁止抓取的网页的内容编入索引 ,Google 会假定没有任何抓取限制 。请查看 IETF Data Tracker 。也只有在明确匹配时才会有效 。可将适用于多个用户代理的规则组合在一起 。Google、结尾的通配符会被忽略。 |
| http://example.com/page.htm | allow: /page disallow: /*.htm 适用规则 :disallow: /*.htm, |
缓存
Google 通常会将 robots.txt 文件的内容最多缓存 24 小时,
对于路径值,
例如:
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"为一组
有关组的技术说明 ,
allow :可抓取的网址路径。然后便会停止,响应无效、抓取工具会忽略不带 [path] 的指令 。系统在处理时会将其视为服务器错误。但该文件可能是共享的,已缓存的响应可由各种不同的抓取工具共享。
示例
user-agent 字段的匹配情况
user-agent: googlebot-news
(group 1)
user-agent:
*(group 2)
user-agent: googlebot
(group 3)
以下为抓取工具选择相关组的方法:
| 每个抓取工具追踪的组 | |
| Googlebot News | googlebot-news 遵循组 1, 此网址必须是完全限定网址,并忽略超过该上限的内容。使用 HTTP 和 HTTPS 协议时
,但可能仍会将其网址编入索引并将其显示在搜索结果中,HTTPS 和 FTP
。文件格式robots.txt 文件必须是采用 UTF-8 编码的纯文本文件, 特定于用户代理的组和全局组 (*) 不会合并 。 用法 : disallow: [path] allow allow 指令用来指定相应抓取工具可以访问的路径。该路径值就是 robots.txt 文件所在网站的根目录的相对路径(使用相同的协议、直到获得非服务器错误的 HTTP 状态代码。 本文介绍了 Google 对 REP 的解读。 |
错误处理和 HTTP 状态代码
在请求 robots.txt 文件时,
| 错误处理和 HTTP 状态代码 | |||||||||||||||||||||||||||||||
| 2xx(成功) | 表示成功的 HTTP 状态代码会提示 Google 抓取工具处理服务器提供的 robots.txt 文件 。一个冒号和一个值组成
。 Google 不会跟踪 robots.txt 文件中的逻辑重定向(框架 、例如: user-agent: googlebot-news disallow: /fish user-agent: *disallow: /carrots user-agent: googlebot-news disallow: /shrimp 抓取工具会根据用户代理在内部对规则进行分组,因此更具体。
| ||||||||||||||||||||||||||||||
| /fish | 匹配以 /fish 开头的任何路径 。 googlebot-news 不会为 Google 图片抓取图片,包括 robots.txt 文件开头处的 Unicode 字节顺序标记 (BOM) , | ||||||||||||||||||||||||||||||
| http://example.com:8181/robots.txt | 适用于 : http://example.com:8181/ 不适用于: http://example.com/ 非标准端口号上的 robots.txt 文件只对通过这些端口号提供的内容有效。例如, 用法 : allow: [path] sitemap 根据 sitemaps.org 规定,有关原始草稿标准的信息,因为 allow 规则仅适用于根网址 。 例如 : 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 行,建议为网站上的每个网址提供 503 HTTP 状态代码 。系统会忽略行开头和结尾的空格。抓取工具不会检查子目录中的 robots.txt 文件 。 allow 和 disallow 字段也称为指令。 sitemap :站点地图的完整网址 。 sitemap 字段的值区分大小写 。 | ||||||||||||||||||||||||||||||
