前言
第 02 篇把 title、description、canonical 配齐了,可爬虫得先知道站上有哪些 URL、哪些路径允许抓。
这一篇拿 5 个站点的快照看这两个文件,快照 2026-11-24:每个地址都有可点击链接,图是当天在浏览器里打开的样子,随时可以自己核对;之后若有改动,以你打开时为准。结论先放这里:robots.txt 管「能不能抓」,sitemap.xml 管「有哪些 URL」,两者都不负责「收不收录」。
大站观察:五类站点的真实形状
先给结论:同一个 /sitemap.xml,五个站给出三种回应:MDN 返回索引文件,nextjs.org 和 cloudflare 返回单文件 XML,掘金和阮一峰返回 404,掘金的真地址写在 robots.txt 里。所以先读 Sitemap: 声明,再去抓。
UA 只定义一次:
UA='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0 Safari/537.36'MDN:索引文件,按语言分片
MDN 的 robots.txt 只有 119 字节,放行所有爬虫、声明 sitemap、禁掉 /api/、/*/files/ 和 /media:
图 1:https://developer.mozilla.org/robots.txt,2026-11-24 抓取。
它的 sitemap.xml 是 1,282 字节的索引,列了 10 个语言子文件,每个都带 lastmod:
图 2:https://developer.mozilla.org/sitemap.xml,2026-11-24 抓取。
curl -sS --max-time 20 -A "$UA" -D - https://developer.mozilla.org/robots.txt -o mdn-robots.txtcurl -sS --max-time 20 -A "$UA" -D - https://developer.mozilla.org/sitemap.xml -o mdn-sitemap.xmlwc -c mdn-sitemap.xmlHTTP/2 200content-type: text/plaincontent-length: 119User-agent: *Sitemap: https://developer.mozilla.org/sitemap.xmlDisallow: /api/Disallow: /*/files/Disallow: /mediaHTTP/2 200content-type: application/xmlcontent-length: 1282 1282 mdn-sitemap.xml<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://developer.mozilla.org/sitemaps/en-us/sitemap.xml.gz</loc><lastmod>2026-11-24</lastmod></sitemap>…内容在子文件里。中文那份解开是 995,005 字节、7,659 条 URL,其中 7,633 条带 lastmod:
curl -sS --max-time 30 -A "$UA" https://developer.mozilla.org/sitemaps/zh-cn/sitemap.xml.gz | gunzip -c > mdn-zh.xmlwc -c mdn-zh.xmlgrep -o '<url>' mdn-zh.xml | wc -lgrep -o '<lastmod>' mdn-zh.xml | wc -l 995005 mdn-zh.xml 7659 7633索引的 lastmod 是 2026-11-24,子文件里 URL 的却早到 2025 年:索引日期说的是「文件最近重写过」,不是「内容昨天更新了」。
nextjs.org:没有 robots.txt,sitemap 有 1,147 条
nextjs.org 的 robots.txt 是 404(113,556 字节 HTML),sitemap.xml 却是 163,947 字节、1,147 条 URL,没有 changefreq 和 priority,每条都带毫秒级 lastmod:
图 3:https://nextjs.org/sitemap.xml,2026-11-24 抓取。
curl -sS --max-time 20 -A "$UA" -o /dev/null -w '%{http_code} %{content_type}\n' https://nextjs.org/robots.txtcurl -sS --max-time 20 -A "$UA" -D - https://nextjs.org/sitemap.xml -o nextjs-sitemap.xmlwc -c nextjs-sitemap.xmlgrep -o '<url>' nextjs-sitemap.xml | wc -lgrep -o '<lastmod>2026-11-20T22:06:22.904Z</lastmod>' nextjs-sitemap.xml | wc -l404 text/html; charset=utf-8HTTP/2 200content-type: application/xml; charset=utf-8 163947 nextjs-sitemap.xml 1147 8411,147 条里 841 条共用 2026-11-20T22:06:22.904Z,不可能上千篇文档同一毫秒改完,那是构建时统一打的。
掘金:/sitemap.xml 是 404,真地址在 robots 里
掘金的 sitemap.xml 返回 404,只有一行 File Not Found;robots.txt 是 200、844 字节,末尾声明了 9 个 sitemap:
图 4:https://juejin.cn/robots.txt,2026-11-24 抓取。滚到底部能看到 9 条 Sitemap 声明。
挑 posts 那个验证,索引文件 6,841 字节,下挂 56 个分片,分片的 lastmod 是 2026-11-24T02:00:54+08:00:
curl -sS --max-time 20 -A "$UA" -o /dev/null -w '%{http_code} %{content_type}\n' https://juejin.cn/sitemap.xmlcurl -sS --max-time 20 -A "$UA" https://juejin.cn/robots.txtcurl -sS --max-time 30 -A "$UA" -D - https://juejin.cn/sitemap/posts/index.xml -o juejin-posts.xml404 text/plain; charset=utf-8User-agent: *Disallow: /subscribe/subscribedDisallow: /user/settingsDisallow: /search…Sitemap: https://juejin.cn/sitemap/posts/index.xmlSitemap: https://juejin.cn/sitemap/user/index.xml…(共 9 条)HTTP/2 200content-length: 6841<sitemapindex …><sitemap><loc>https://juejin.cn/sitemap/posts/index1.xml</loc><lastmod>2026-11-24T02:00:54+08:00</lastmod></sitemap>…56 个分片说明单文件放不下全站文章,这就是 sitemapindex 的理由。
cloudflare.com:AI 爬虫段与被忽略的字段
cloudflare 的 robots.txt 是 1,099 字节、46 行,给 GPTBot、Google-Extended、CCBot 等逐个 Allow: /,还写了一行 Content-Signal: ai-train=yes, search=yes, ai-input=yes:
图 5:https://www.cloudflare.com/robots.txt,2026-11-24 抓取。下半段是 AI 爬虫声明。
它的 sitemap.xml 是 1,743,470 字节、1,069 条 URL,每条都带 changefreq 和 priority:
curl -sS --max-time 20 -A "$UA" https://www.cloudflare.com/robots.txtcurl -sS --max-time 40 -A "$UA" https://www.cloudflare.com/sitemap.xml -o cf-sitemap.xmlgrep -o '<changefreq>[^<]*</changefreq>' cf-sitemap.xml | sort | uniq -cgrep -o '<priority>[^<]*</priority>' cf-sitemap.xml | sort | uniq -cContent-Signal: ai-train=yes, search=yes, ai-input=yesUser-agent: Google-ExtendedAllow: /content-length: 1743470 977 <changefreq>monthly</changefreq> 89 <changefreq>weekly</changefreq> 3 <changefreq>daily</changefreq> 946 <priority>0.6</priority> 79 <priority>0.8</priority>字段齐,但这两个早被 Google 忽略。
阮一峰:0 字节的 robots.txt
最后一个样本零配置:robots.txt 返回 200、content-length: 0,打开是空白页;sitemap.xml 返回 404。
图 6:https://www.ruanyifeng.com/robots.txt,2026-11-24 抓取。文件是空的,所以页面全白。
curl -sS --max-time 20 -A "$UA" -D - -o /dev/null https://www.ruanyifeng.com/robots.txtcurl -sS --max-time 20 -A "$UA" -D - -o /dev/null https://www.ruanyifeng.com/sitemap.xmlHTTP/2 200content-type: text/plaincontent-length: 0HTTP/2 404content-type: text/html; charset=iso-8859-1两个文件都可选;真正决定能不能抓的是页面返回 200。
判读:robots 管放行,sitemap 管发现
先给结论:8 个观察项逐条对照官方文档,4 项符合、2 项不符合、1 项已失效、1 项只是声明。
| 观察项 | 判定 |
|---|---|
| MDN:robots 声明 sitemap,索引分 10 个语言子文件 | 符合 |
| nextjs.org:无 robots,sitemap 1,147 条 | 符合 |
掘金:/sitemap.xml 404,robots 声明真地址 | 符合 |
cloudflare:1,069 条全写 changefreq、priority | 已失效 |
cloudflare:Content-Signal 与 AI 段 | 声明,未执行 |
nextjs.org:lastmod 是构建时间;MDN:sitemap 含 /zh-CN/404 | 不符合 |
| 阮一峰:空 robots、无 sitemap | 符合 |
robots.txt 只对同协议、同主机、同端口的 URL 生效,也不是访问控制;Disallow 只表示「别来抓」不表示「别收录」,抓不到内容时搜索系统仍可能因外链收录 URL,只是没有摘要。
noindex 与 robots 有先后:要阻止收录,得让爬虫抓到页面看到 noindex;robots 先把抓取挡了,noindex 就读不到。所以预览环境要么彻底不给访问(401/403 或登录),要么放爬虫进来返回 noindex。
robots 与 noindex 的配合顺序想让页面不进索引,顺序是「允许抓取」加「返回 noindex」;用 robots 挡住抓取,
noindex就读不到。预览环境优先访问控制。
priority、changefreq 已被 Google 忽略,lastmod 只在「一致且可验证」时采用;单文件上限 50,000 条 / 50 MB,超过用 sitemapindex。IndexNow 返回 200 只表示已接收,202/422/429 是 key 待验证、不符、过频,单次最多 10,000 条;Bing 旧 SOAP API 2026-08-31 退役。
控制训练用 Google-Extended,只作用于 Gemini 训练与 grounding,不影响 Search;2026-10-30 提出的 AIPREF 共享控制,Google 计划 2027 年支持,在那之前 Content-Signal 只能当声明看。llms.txt Google Search 不用。
反例:大站也会留破绽
第一个反例在 MDN 的 sitemap 里。/zh-CN/404 就躺在那份 7,659 条 URL 的子 sitemap 中,抓下来是 HTTP 200、标题「Page not found | MDN」,head 里写着 noindex,nofollow:
curl -sS --max-time 20 -A "$UA" -D - https://developer.mozilla.org/zh-CN/404 -o mdn-404.htmlgrep -o '<meta[^>]*name="robots"[^>]*>' mdn-404.htmlgrep -o '<title>[^<]*</title>' mdn-404.htmlHTTP/2 200content-type: text/htmlcontent-length: 88425<meta name="robots" content="noindex, nofollow" /><title>Page not found | MDN</title>一条 URL 的影响可以忽略,但它暴露了生成逻辑:按路由枚举,不按「可索引的规范 URL」过滤,草稿页、错误页也会混进来。
第二个反例是 cloudflare:1,069 条 URL 里 977 条标 monthly、89 条 weekly、3 条 daily,priority 集中在 0.6 和 0.8;这些字段 Google 全部忽略。
第三个反例是 nextjs.org 的 lastmod:1,147 条里 841 条共用同一个毫秒级时间戳,只能是构建时间;全站统一刷恰好落在「不可验证」那一侧。
nextjs.org 没 robots.txt、掘金 /sitemap.xml 404 都不算错:判断配置不能只敲一个 URL。
照抄清单:四个入口一次配好
先给结论:能被稳定发现的站只要四个入口——robots.txt、sitemap.xml、RSS 和 IndexNow,都从同一份内容索引出发。
robots.txt 按环境生成
生产放行并声明 sitemap,预览整站禁止。本博客用 TanStack Router 的 server route,有生产域名就放行,没有就禁止:
import { createFileRoute } from '@tanstack/react-router'import { SITE_ORIGIN } from '#/lib/seo'export const Route = createFileRoute('/robots.txt')({ server: { handlers: { GET: () => new Response( SITE_ORIGIN ? `User-agent: *\nDisallow:\nSitemap: ${SITE_ORIGIN}/sitemap.xml\n` : 'User-agent: *\nDisallow: /\n', { headers: { 'Content-Type': 'text/plain; charset=utf-8' }, }, ), }, },})Next.js 用 app/robots.ts 按环境变量区分,host 告诉爬虫规范域名,sitemap 就是那行声明:
import type { MetadataRoute } from 'next'const BASE = 'https://example.com'const isProduction = process.env.NODE_ENV === 'production'export default function robots(): MetadataRoute.Robots { if (!isProduction) { return { rules: [{ userAgent: '*', disallow: '/' }] } } return { rules: [{ userAgent: '*', allow: '/', disallow: ['/api/', '/drafts/'] }], sitemap: `${BASE}/sitemap.xml`, host: BASE, }}Disallow: / 只挡抓取不挡收录,预览域名最稳是在边缘加 Basic Auth 或返回 401。
sitemap 从内容索引生成
sitemap 每条 URL 都要满足:返回 200、是 canonical 地址、允许索引。Next.js 用 app/sitemap.ts,lastModified 取真实更新时间:
import type { MetadataRoute } from 'next'import { posts } from '@/lib/content'const BASE = 'https://example.com'export default function sitemap(): MetadataRoute.Sitemap { return [ { url: `${BASE}/`, lastModified: new Date('2026-11-24') }, ...posts.map((post) => ({ url: `${BASE}/blog/${post.slug}`, lastModified: new Date(post.updatedAt), })), ]}TanStack Router 这一侧,本博客的 src/routes/sitemap[.]xml.ts 调 renderSitemap 生成 XML:
import { createFileRoute } from '@tanstack/react-router'import { renderSitemap } from '#/lib/publication'import { SITE_ORIGIN } from '#/lib/seo'import { publicationPages } from '#/lib/site'import { listCategories, listPosts, listTags } from '#/services/posts'export const Route = createFileRoute('/sitemap.xml')({ server: { handlers: { GET: ({ request }) => new Response( renderSitemap( SITE_ORIGIN || new URL(request.url).origin, publicationPages(listPosts(), listCategories(), listTags()), ), { headers: { 'Content-Type': 'application/xml; charset=utf-8' }, }, ), }, },})截至 2026-11-24,renderSitemap 只输出 <loc>,没有 lastmod;不写不是错误,只是放弃了「最近改过」的信号。要补就带真实 updatedAt;超过 50,000 条或 50 MB 改用 sitemapindex。
RSS 不是 sitemap 的替代
RSS 给读者和聚合器,sitemap 给爬虫,数据源可以是同一份。本博客的 src/routes/rss[.]xml.ts 用 renderRss 输出最新文章,和 sitemap 共用 listPosts()。
IndexNow:发布后通知,别等收录
URL 变了才通知,别每次构建都发。发布成功后调一次,单次最多 10,000 个:
curl -sS -X POST 'https://api.indexnow.org/indexnow' \ -H 'Content-Type: application/json; charset=utf-8' \ -d '{ "host": "example.com", "key": "<你的 key>", "keyLocation": "https://example.com/<你的 key>.txt", "urlList": ["https://example.com/blog/hello", "https://example.com/blog/world"] }'返回 200 只说明「已接收」,不代表已收录;202 检查 key 文件、422 是 key 不符、429 是发得太频。
自查:把命令跑在自己的站上
先给结论:七行检查全是免账号命令,任意静态站都能跑。把 example.com 换成你的域名:
| # | 检查 | 命令 | 通过标准 |
|---|---|---|---|
| 1 | robots 可访问且声明 sitemap | curl -sS https://example.com/robots.txt | 200,含 Sitemap: |
| 2 | sitemap 可解析 | curl -sS -o /dev/null -w '%{http_code} %{content_type}\n' https://example.com/sitemap.xml | 200,application/xml |
| 3 | URL 都是 200 | 抽 20 条逐条 curl -o /dev/null -w '%{http_code}\n' | 无 301/302/404 |
| 4 | canonical 自指 | 抓 head 看 link rel="canonical" | 等于当前 URL |
| 5 | sitemap 无 noindex 页 | 抽 5 条看 meta name="robots" | 不含 noindex |
| 6 | lastmod 是真修改时间 | 对比 lastmod 与文章 updatedAt | 一致,非全站同值 |
| 7 | 规模在限制内 | grep -o '<url>' sitemap.xml | wc -l | ≤50,000 条、≤50 MB |
读结果三个动作:sitemap 返回 404,先去 robots.txt 找 Sitemap: 声明;sitemap 里的 URL 返回 301 或 404,说明生成逻辑按路由枚举;全站 URL 共用同一个 lastmod,基本是构建时间。
有 Search Console 账号再看两处:Sitemaps 报告里「已读取」与「已发现的 URL 数」是否对得上,索引编制下「网页」报告里被排除的原因。
总结
这一篇处理「发现」:robots.txt 决定哪些路径允许抓,sitemap.xml 告诉爬虫有哪些 URL 值得抓,两者都不负责收录结果。
判断标准只有一句话:robots 写「允许或禁止抓哪些路径」,sitemap 写「规范 URL 加真实修改时间」;超出这个范围的期待都会落空。
两个坑再点一次:robots 的 Disallow 不是收录开关,被挡住的页面连 noindex 都读不到;sitemap 的 priority 和 changefreq 已被忽略,lastmod 要写真实的修改时间。
下一篇讲同一份内容只留一个地址:URL、状态码与重定向。robots 和 sitemap 把爬虫领进门后,第一步就是检查它拿到的是不是 canonical 地址。