一、什么是 robots.txt?
robots.txt 是一个放在网站根目录下的纯文本文件,用于告诉搜索引擎爬虫(Robot/Spider/Crawler)哪些页面可以抓取,哪些不可以。
它诞生于 1994 年的 Robots Exclusion Protocol(机器人排除协议),虽然不是国际标准,但已被 Google、Bing、百度等主流搜索引擎广泛支持。
核心作用
| 作用 | 说明 |
|---|---|
| 隐私保护 | 阻止爬虫抓取后台、用户数据等敏感页面 |
| SEO 优化 | 避免低质量页面被索引,集中权重到核心内容 |
| 带宽节省 | 减少爬虫对服务器资源的无效消耗 |
| 抓取引导 | 通过 Sitemap 告诉爬虫网站结构 |
二、文件位置与基本格式
📍 必须放在根目录
arduino
https://example.com/robots.txt ✅ 正确
https://example.com/path/robots.txt ❌ 错误,搜索引擎只认根目录
📄 基本语法结构
txt
User-agent: [爬虫名称]
Disallow: [禁止抓取的路径]
Allow: [允许抓取的路径]
Sitemap: [站点地图地址]
三、指令详解与实战示例
1. User-agent --- 指定爬虫身份
txt
# 针对所有爬虫
User-agent: *
# 只针对 Google
User-agent: Googlebot
# 只针对百度
User-agent: Baiduspider
常见爬虫名称对照表:
| 搜索引擎 | 爬虫名称 | 说明 |
|---|---|---|
Googlebot |
网页抓取 | |
Googlebot-Image |
图片搜索 | |
| 百度 | Baiduspider |
网页抓取 |
| 百度 | Baiduspider-image |
图片搜索 |
| Bing | Bingbot |
微软必应 |
| 搜狗 | Sogou spider |
搜狗搜索 |
| 360 | 360Spider |
360搜索 |
2. Disallow --- 禁止抓取
txt
# 禁止抓取整个网站(不推荐,除非特殊场景)
User-agent: *
Disallow: /
# 禁止抓取后台目录
Disallow: /admin/
# 禁止抓取所有 .php 文件
Disallow: /*.php$
# 禁止抓取包含 ? 的 URL(通常是动态参数)
Disallow: /*?
3. Allow --- 允许抓取(覆盖 Disallow)
txt
# 禁止抓取 /private/ 目录,但允许抓取其中的 public 子目录
User-agent: *
Disallow: /private/
Allow: /private/public/
⚠️ 注意:Allow 优先级高于 Disallow(Google 支持,部分搜索引擎可能不支持)。
4. Sitemap --- 提交站点地图
txt
Sitemap: https://example.com/sitemap.xml
Sitemap: https://example.com/sitemap2.xml
可以放置多个 Sitemap,帮助爬虫更快发现网站内容。
四、高级匹配规则与通配符
通配符使用
| 符号 | 含义 | 示例 |
|---|---|---|
* |
匹配任意字符序列 | Disallow: /*.pdf$ |
$ |
表示 URL 结尾 | Disallow: /*.jpg$ |
实战示例:复杂规则配置
txt
# ============================================
# 示例:电商网站 robots.txt
# ============================================
# 规则1:针对所有爬虫的基础规则
User-agent: *
Disallow: /admin/ # 后台管理
Disallow: /cart/ # 购物车
Disallow: /checkout/ # 结算页面
Disallow: /user/ # 用户中心
Disallow: /search/ # 站内搜索结果(避免重复内容)
Disallow: /*?sort= # 排序参数产生的重复页面
Disallow: /*?page= # 分页参数(如果有独立分页规则)
Disallow: /api/ # 内部 API
Disallow: /*.pdf$ # PDF 文件
Disallow: /*.zip$ # 压缩包
# 但允许抓取商品详情和分类
Allow: /product/
Allow: /category/
# 规则2:针对 Google 图片搜索的特殊规则
User-agent: Googlebot-Image
Disallow: # 允许抓取所有图片
Allow: /uploads/images/
# 规则3:禁止恶意爬虫
User-agent: BadBot
Disallow: /
# 站点地图
Sitemap: https://example.com/sitemap.xml
五、常见误区与陷阱
❌ 误区 1:robots.txt 能保护隐私
真相 :robots.txt 只是"建议",不是"强制"。恶意爬虫完全可以无视它。
txt
# 错误的隐私保护方式
Disallow: /secret-page.html
# 虽然合规爬虫不会抓,但所有人都能看到你的"秘密"路径!
✅ 正确做法 :敏感内容使用 HTTP 认证 、IP 白名单 或 noindex 元标签。
❌ 误区 2:Disallow 后页面就不会出现在搜索结果
真相:如果其他网站链接了这个页面,它仍可能出现在搜索结果中(只是没有描述)。
✅ 正确做法:对不想被索引的页面,同时使用:
html
<!-- 在页面 <head> 中添加 -->
<meta name="robots" content="noindex, nofollow">
❌ 误区 3:用 robots.txt 屏蔽 CSS/JS 文件
真相:Google 需要渲染页面,屏蔽资源会影响 SEO 评分。
✅ 正确做法 :不要屏蔽 /css/、/js/ 目录。
❌ 误区 4:规则顺序无所谓
真相 :规则匹配是从上到下,以第一个匹配的规则为准。
txt
# ❌ 错误:Allow 永远不会生效
User-agent: *
Disallow: /
Allow: /public/
# ✅ 正确:先允许,再禁止
User-agent: *
Allow: /public/
Disallow: /
六、验证与测试工具
1. Google Search Console
访问 Google Search Console → 抓取 → robots.txt 测试工具
2. 在线验证工具
- Google Robots Testing Tool
- 百度搜索资源平台
3. 手动验证
直接访问 https://your-site.com/robots.txt 检查内容。
七、最佳实践清单
| ✅ 应该做 | ❌ 不应该做 |
|---|---|
| 明确指定 Sitemap | 用 robots.txt 保护敏感数据 |
| 屏蔽低价值页面(搜索页、过滤页) | 屏蔽 CSS/JS 文件 |
| 为不同爬虫设置差异化规则 | 在 robots.txt 中暴露后台路径 |
| 定期检查和更新规则 | 使用复杂的正则表达式(兼容性差) |
配合 <meta name="robots"> 使用 |
期望所有爬虫都遵守规则 |
八、完整模板参考
txt
# ============================================
# robots.txt for example.com
# Last updated: 2026-07-15
# ============================================
# 全局规则
User-agent: *
Disallow: /admin/
Disallow: /api/
Disallow: /tmp/
Disallow: /search?
Disallow: /*?*sort=
Disallow: /*?*filter=
Allow: /
# 允许图片爬虫
User-agent: Googlebot-Image
User-agent: Baiduspider-image
Allow: /uploads/
Allow: /static/images/
# 禁止已知的恶意爬虫
User-agent: MJ12bot
User-agent: AhrefsBot
Disallow: /
# 站点地图
Sitemap: https://example.com/sitemap.xml
Sitemap: https://example.com/sitemap-news.xml
九、总结
| 要点 | 核心记忆 |
|---|---|
| 本质 | 爬虫的"交通规则",非强制法律 |
| 位置 | 必须放在网站根目录 |
| 隐私 | 不能替代身份验证,只是防君子不防小人 |
| SEO | 配合 noindex 使用,引导而非阻止索引 |
| 维护 | 定期审查,特别是网站重构后 |
💡 一句话总结 :
robots.txt是网站与搜索引擎的"君子协定"------它告诉爬虫该去哪里,但无法阻止恶意访问。真正的安全,需要结合服务器层防护。
希望这篇指南能帮你彻底掌握 robots.txt!如果有具体的业务场景需要定制规则,欢迎在评论区交流 👇