每个网站的根目录下都有一个纯文本文件,决定了搜索引擎爬虫能去哪、不能去哪。这个文件就是 robots.txt——它不是防火墙,不是密码锁,而是网站和爬虫之间的一份访客守则。
守则的本质是 "君子协定":遵守它的爬虫(如 Googlebot)会照做,不遵守的爬虫(如多数恶意抓取器)直接无视。理解这个前提,才不会对 robots.txt 产生错误的安全期待。
但 robots.txt 只是爬虫控制的三件套之一。另外两个——Meta Robots 标签和 X-Robots-Tag HTTP 头——各有适用场景。这篇指南一次性讲清三者的语法、区别、优先级,并重点覆盖 2026 年最热的议题:AI 爬虫到底该不该屏蔽。
robots.txt 基础语法
robots.txt 必须放在网站根目录(https://example.com/robots.txt),文件名全小写,编码 UTF-8。每个子域名需要独立的 robots.txt——blog.example.com 和 www.example.com 是两个不同的站。
四个核心指令
| 指令 | 作用 | 示例 |
|---|---|---|
User-agent |
指定规则适用的爬虫 | User-agent: Googlebot |
Disallow |
禁止抓取的路径 | Disallow: /admin/ |
Allow |
在 Disallow 范围内开放特定路径 | Allow: /admin/public/ |
Sitemap |
声明站点地图位置 | Sitemap: https://example.com/sitemap.xml |
规则匹配顺序:Google 使用 "最具体的路径优先" 原则。如果 Disallow: /admin/ 和 Allow: /admin/public/ 同时存在,/admin/public/page.html 会被允许抓取,因为 Allow 路径更长、更具体。
通配符与结束符
虽然 robots.txt 的原始标准(1994 年)不包含通配符,但 Google 和 Bing 都支持两个扩展:
*匹配任意长度字符序列:Disallow: /*.pdf$屏蔽所有 PDF 文件$表示路径结束:Disallow: /search$只屏蔽/search,不影响/search?q=test或/search/results
一个完整的 robots.txt 最小示例:
# 所有爬虫的默认规则
User-agent: *
Disallow: /admin/
Disallow: /cart/
Disallow: /checkout/
Allow: /admin/public/
# 站点地图
Sitemap: https://example.com/sitemap.xml
几个容易踩的语法坑:
Disallow:(冒号后为空)= 允许抓取所有路径,不是禁止所有Disallow: /= 禁止抓取整站- 路径区分大小写:
Disallow: /Admin/不会屏蔽/admin/ - 每条指令独占一行,不支持一行写多个路径
常见配置模板
以下模板可直接复制使用,根据实际路径调整即可。
电商站(Shopify / WooCommerce)
User-agent: *
# 购物车和结账流程
Disallow: /cart
Disallow: /checkout
Disallow: /orders
# 站内搜索结果(避免无限参数组合被索引)
Disallow: /search
Disallow: /*?q=*
# 用户账户页
Disallow: /account
# 筛选和排序参数页(避免重复内容)
Disallow: /*?sort_by=*
Disallow: /*?filter.*
# 允许 CSS/JS(确保渲染正常)
Allow: /*.css$
Allow: /*.js$
Sitemap: https://example.com/sitemap.xml
博客站 / 内容站
User-agent: *
# 后台管理
Disallow: /admin/
Disallow: /wp-admin/
Allow: /wp-admin/admin-ajax.php
# 标签和分页的深层页面(可选,防止低质聚合页被抓取)
Disallow: /tag/*/page/
Disallow: /author/
# 内部搜索
Disallow: /search/
Disallow: /?s=
Sitemap: https://example.com/sitemap.xml
多语言站(hreflang 架构)
User-agent: *
Disallow: /admin/
Disallow: /cart/
# 不要屏蔽任何语言子目录——每个版本都需要被抓取
# 错误做法:Disallow: /zh/ 或 Disallow: /ja/
Sitemap: https://example.com/sitemap-index.xml
多语言站常见错误是把非主要语言目录写进 Disallow。这会导致 hreflang 标签失效,搜索引擎无法正确关联各语言版本。
SPA 单页应用
User-agent: *
Disallow: /api/
Disallow: /_next/data/
# 关键:确保所有静态资源可访问
Allow: /_next/static/
Allow: /*.js$
Allow: /*.css$
Allow: /*.png$
Allow: /*.jpg$
Allow: /*.svg$
Sitemap: https://example.com/sitemap.xml
SPA 站点如果屏蔽了 JS 和 CSS,Googlebot 的渲染引擎(WRS, Web Rendering Service)无法执行页面逻辑,只能看到空壳 HTML。对于依赖客户端渲染的站点,建议阅读 JS 渲染与 SEO 的完整指南。
Meta Robots 标签
robots.txt 控制 "抓不抓取",Meta Robots 标签控制 "索不索引" 和 "跟不跟链接"。两者解决的是完全不同的问题。
Meta Robots 是放在 HTML <head> 中的标签:
<meta name="robots" content="noindex, nofollow">
指令一览
| 指令 | 效果 | 默认值 |
|---|---|---|
index |
允许索引此页面 | 默认 |
noindex |
不索引此页面 | — |
follow |
跟踪页面上的链接 | 默认 |
nofollow |
不跟踪页面上的链接 | — |
noarchive |
不显示缓存版本 | — |
nosnippet |
不显示摘要文字 | — |
max-snippet:[n] |
摘要最多显示 n 个字符 | 无限制 |
max-image-preview:[size] |
图片预览大小:none / standard / large | 由引擎决定 |
max-video-preview:[n] |
视频预览最多 n 秒 | 无限制 |
notranslate |
不在 SERP 提供翻译链接 | — |
多个指令用逗号分隔。常见组合:
noindex, follow:不收录页面,但跟踪页面上的链接(适合标签归档页)index, nofollow:收录页面,但不传递链接权重(适合用户生成内容页面)noindex, nofollow:既不收录也不跟踪(适合内部测试页、感谢页)max-snippet:160, max-image-preview:large:控制 SERP 展示形式(不影响索引)
关于 nofollow 的更多细节和使用场景,可以参考我们的专题文章。
针对特定爬虫的 Meta Robots
name 属性可以指定特定爬虫:
<!-- 仅对 Googlebot 生效 -->
<meta name="googlebot" content="noindex">
<!-- 仅对 Bingbot 生效 -->
<meta name="bingbot" content="noarchive">
当 name="robots" 和 name="googlebot" 同时存在且冲突时,Google 采用更严格的那个。比如 robots 说 index、googlebot 说 noindex,最终结果是 noindex。
X-Robots-Tag HTTP 头
PDF、图片、视频、JSON 文件没有 HTML <head>,无法放 Meta Robots 标签。X-Robots-Tag 通过 HTTP 响应头实现同样的控制。
在 Nginx 中对 PDF 文件设置 noindex:
location ~* \.pdf$ {
add_header X-Robots-Tag "noindex, nofollow";
}
在 Apache 的 .htaccess 中:
<FilesMatch "\.pdf$">
Header set X-Robots-Tag "noindex, nofollow"
</FilesMatch>
X-Robots-Tag 支持所有 Meta Robots 的指令,也支持指定爬虫:
X-Robots-Tag: googlebot: noindex
X-Robots-Tag: bingbot: noarchive
实际应用场景:
- 内部白皮书 PDF 不想被索引但需要对外提供下载链接
- API 返回的 JSON 不想出现在搜索结果中
- CDN 上的图片需要限制缩略图展示
robots.txt vs Meta Robots vs X-Robots-Tag 对比
| 维度 | robots.txt | Meta Robots | X-Robots-Tag |
|---|---|---|---|
| 作用层级 | 路径 / 目录级 | 页面级 | 资源级 |
| 控制对象 | 抓取(crawl) | 索引 + 展示 | 索引 + 展示 |
| 实现方式 | 根目录文本文件 | HTML <head> 标签 | HTTP 响应头 |
| 适用资源 | 所有 URL | 仅 HTML 页面 | 所有资源类型 |
| 能阻止抓取? | 能 | 不能 | 不能 |
| 能阻止索引? | 不能 | 能 | 能 |
| 需要被抓取才生效? | 否(先读取后决定抓取) | 是 | 是 |
关键理解:robots.txt 的 Disallow 阻止的是抓取,不是索引。如果一个页面被 robots.txt 屏蔽,但有外部链接指向它,Google 仍然可能在搜索结果中显示这个 URL(只是没有摘要)。要真正阻止页面出现在搜索结果中,必须用 noindex——但 noindex 又要求页面能被抓取到才能读取到这条指令。
这就引出一个常见的死锁:robots.txt 屏蔽了页面 + 页面上写了 noindex = noindex 永远不会被读到。如果你想让一个页面从索引中消失,要么只用 noindex(不要在 robots.txt 中屏蔽它),要么用 X-Robots-Tag 在 HTTP 头层面处理。关于这个问题的实际排查案例,可以参考 robots.txt 屏蔽导致的索引问题处理。
AI 爬虫管理(2026 重点)
2024-2025 年,AI 公司的爬虫开始大规模抓取互联网内容用于模型训练和 RAG 检索。到 2026 年,管理 AI 爬虫已经成为 robots.txt 配置中最重要的新议题。
主流 AI 爬虫 User-agent 清单
| AI 服务 | User-agent | 用途 |
|---|---|---|
| OpenAI (ChatGPT) | GPTBot |
模型训练 + 搜索 |
| OpenAI (搜索) | ChatGPT-User |
实时搜索浏览 |
| Anthropic (Claude) | ClaudeBot |
模型训练 |
| Anthropic (搜索) | ClaudeBot-SearchPreview |
搜索预览 |
| Google (AI 训练) | Google-Extended |
Gemini 模型训练 |
| Perplexity | PerplexityBot |
AI 搜索 |
| Meta | Meta-ExternalAgent |
AI 训练 |
| Apple | Applebot-Extended |
Apple Intelligence 训练 |
| Common Crawl | CCBot |
开放数据集(被多家 AI 使用) |
| Bytedance | Bytespider |
抓取 + AI 训练 |
屏蔽 AI 训练爬虫但允许 AI 搜索的模板
多数网站的合理策略是:允许 AI 搜索引擎抓取(获得流量),屏蔽纯训练用途的爬虫(保护内容资产)。
# === AI 爬虫管理 ===
# 屏蔽纯训练用途
User-agent: GPTBot
Disallow: /
User-agent: ClaudeBot
Disallow: /
User-agent: Google-Extended
Disallow: /
User-agent: Meta-ExternalAgent
Disallow: /
User-agent: Applebot-Extended
Disallow: /
User-agent: CCBot
Disallow: /
User-agent: Bytespider
Disallow: /
# 允许 AI 搜索引擎的实时抓取
User-agent: ChatGPT-User
Allow: /
User-agent: PerplexityBot
Allow: /
# === 传统搜索引擎 ===
User-agent: *
Disallow: /admin/
Disallow: /cart/
Sitemap: https://example.com/sitemap.xml
该不该屏蔽 AI 爬虫?
这不是一个非黑即白的问题。取决于你的内容类型和商业模式:
倾向屏蔽的场景:
- 内容本身就是产品(付费课程、研究报告、数据库)——被免费训练等于被免费复制
- 新闻媒体——内容时效性强,被 AI 直接回答后用户不再点进原文
- 已经有稳定的搜索流量,不需要 AI 搜索带来的增量
倾向开放的场景:
- B2B 企业——AI 搜索中被推荐能直接影响采购决策
- 品牌内容站——需要在 AI 回答中建立品牌认知
- 新站或中小站——AI 搜索是高质量爬虫的新流量入口
一个折中方案是选择性开放:允许 AI 搜索爬虫(ChatGPT-User、PerplexityBot)抓取公开内容,屏蔽纯训练爬虫(GPTBot、ClaudeBot、Google-Extended)。同时可以通过 llms.txt 主动引导 AI 模型优先读取你最重要的页面。
值得注意的是:robots.txt 对 AI 爬虫的约束力比传统搜索引擎更弱。部分 AI 公司的爬虫早期并不遵守 robots.txt,在舆论压力下才逐步补上合规机制。屏蔽 robots.txt 只能挡住 "守规矩" 的爬虫,真正的内容保护还需要结合服务器端的 User-agent 检测和访问频率限制。
常见错误与排查
错误一:屏蔽 CSS/JS 导致渲染失败
Googlebot 需要加载页面的 CSS 和 JavaScript 才能正确渲染内容。如果 robots.txt 屏蔽了静态资源目录:
# 错误写法
User-agent: *
Disallow: /static/
Disallow: /assets/
Googlebot 看到的是一个没有样式、没有动态内容的裸 HTML。这会严重影响排名,尤其对 SPA 和重 JS 的站点。在 Google Search Console 的 "URL 检查" 工具中,用 "实际测试" 功能查看 Google 渲染后的截图,可以快速发现这类问题。
错误二:用 Disallow 代替 noindex
最常见的误解。Disallow: /private-page 只是告诉爬虫不要抓取,但如果这个 URL 有外部链接指向它,Google 仍然可能索引这个 URL 并在搜索结果中显示。要让页面从搜索结果中消失,用 noindex。
错误三:robots.txt 更新后不生效
Google 会缓存 robots.txt,缓存周期通常在 24 小时左右,但在某些情况下可能更长。更新后的处理步骤:
- 在 Google Search Console 的 "robots.txt 测试工具" 中提交更新后的文件
- 确认文件语法正确,目标路径被正确匹配
- 等待 24-48 小时让 Google 重新获取
如果 robots.txt 返回 5xx 错误,Google 的处理方式是:在一段时间内继续使用上次成功获取的缓存版本;如果持续无法获取(超过 30 天),Google 会假设没有限制,抓取所有路径。
错误四:多条 User-agent 规则冲突
Google 只执行最具体的那一组规则。如果你同时写了 User-agent: * 和 User-agent: Googlebot,Googlebot 只看 User-agent: Googlebot 下面的规则,完全忽略 User-agent: *。
# Googlebot 只看这组规则,不看 User-agent: * 的规则
User-agent: Googlebot
Disallow: /tmp/
# 其他爬虫看这组
User-agent: *
Disallow: /admin/
Disallow: /tmp/
这意味着如果你想让 Googlebot 也执行通用规则(如屏蔽 /admin/),必须在 User-agent: Googlebot 下面重复写一遍。
排查工具
- Google Search Console — robots.txt 测试工具:验证任意 URL 是否被 robots.txt 屏蔽
- Google Search Console — URL 检查:查看 Google 实际看到的页面渲染结果
- Google 富结果测试:检查结构化数据在受限资源下是否正常
- Screaming Frog:批量检测哪些页面被 robots.txt 屏蔽、哪些返回了 noindex
关于更系统的技术 SEO 排查流程,可参考技术 SEO 完整指南。
完整配置示例
以下是一个中型电商站的完整 robots.txt 模板,整合了传统搜索引擎优化和 AI 爬虫管理:
# =============================================
# robots.txt — example.com
# 最后更新:2026-03-05
# =============================================
# --- AI 训练爬虫:屏蔽 ---
User-agent: GPTBot
Disallow: /
User-agent: ClaudeBot
Disallow: /
User-agent: Google-Extended
Disallow: /
User-agent: CCBot
Disallow: /
User-agent: Meta-ExternalAgent
Disallow: /
User-agent: Bytespider
Disallow: /
User-agent: Applebot-Extended
Disallow: /
# --- AI 搜索爬虫:允许 ---
User-agent: ChatGPT-User
Allow: /
Disallow: /account/
Disallow: /checkout/
User-agent: PerplexityBot
Allow: /
Disallow: /account/
Disallow: /checkout/
# --- 传统搜索引擎 ---
User-agent: *
Disallow: /cart
Disallow: /checkout/
Disallow: /account/
Disallow: /orders/
Disallow: /search
Disallow: /*?sort_by=*
Disallow: /*?filter.*
Disallow: /admin/
Allow: /*.css$
Allow: /*.js$
Allow: /*.png$
Allow: /*.jpg$
Allow: /*.webp$
Allow: /*.svg$
# --- 站点地图 ---
Sitemap: https://example.com/sitemap.xml
翼果洞察
robots.txt 在 1994 年诞生时只需要处理一件事:告诉搜索引擎的爬虫别碰哪些页面。三十年后,它需要同时应对传统搜索引擎、AI 训练爬虫、AI 搜索爬虫三类完全不同的抓取需求。
我们的建议是分三层管理:robots.txt 管抓取权限,Meta Robots / X-Robots-Tag 管索引权限,llms.txt 管 AI 优先级。robots.txt 决定谁能进门,noindex 决定谁能上架,llms.txt 决定 AI 先看哪个货架。三者各司其职,缺一层你就漏一层控制。
另一个值得注意的趋势:2026 年的 robots.txt 管理已经从 "配一次就忘" 变成了持续运营。新的 AI 爬虫 User-agent 每几个月就会出现,不定期审查和更新 robots.txt 正在成为技术 SEO 的常规工作项。