# 站点地图与 robots

URL: https://caijiao.org/google-search-central/04-sitemaps-robots
Source: docs/google-search-central/04-sitemaps-robots.md
Description: 讲解 XML Sitemap、robots.txt、抓取控制和站点 URL 发现策略，帮助网站把重要页面稳定提交给 Google。

站点地图和 `robots.txt` 都和抓取有关，但用途完全不同：Sitemap 帮助搜索系统发现你希望被抓取的重要 URL，`robots.txt` 则用来声明哪些路径不希望被抓取。

## XML Sitemap

最常见的 Sitemap 是 XML 文件：

```xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://example.com/</loc>
    <lastmod>2026-09-23</lastmod>
  </url>
  <url>
    <loc>https://example.com/google-search-central/</loc>
    <lastmod>2026-09-23</lastmod>
  </url>
</urlset>
```

Sitemap 中应该放规范 URL，而不是所有可能的 URL。不要把以下页面提交进去：

- 被 `noindex` 标记的页面。
- 返回 404、5xx 或需要登录的页面。
- canonical 指向其他 URL 的重复页面。
- 搜索结果页、筛选参数页、测试页。

## robots.txt

`robots.txt` 位于站点根目录：

```text
User-agent: *
Disallow: /admin/
Disallow: /search
Sitemap: https://example.com/sitemap.xml
```

它适合阻止抓取无意义或消耗资源的路径，例如管理后台、站内搜索结果和大量参数组合。

它不适合用来隐藏敏感信息。只要 URL 公开可访问，就不应该把密钥、用户隐私或内部数据放在页面里。

## robots 与 noindex 的区别

| 方法 | 作用 | 适合场景 |
| --- | --- | --- |
| `robots.txt Disallow` | 阻止抓取路径 | 后台、搜索结果页、无限参数页 |
| `meta robots noindex` | 允许抓取但不要索引 | 可访问但不希望进入搜索结果的页面 |
| HTTP `X-Robots-Tag` | 对 HTML 或非 HTML 资源控制索引 | PDF、图片、接口响应、批量规则 |

如果页面已经被 `robots.txt` 阻止，Googlebot 可能无法读取页面里的 `noindex`。因此，想从索引中移除页面时，通常应先允许抓取并提供 `noindex`，等状态稳定后再决定是否阻止抓取。

## 多 Sitemap

大型站点可以使用 Sitemap 索引文件，把不同类型的 URL 拆开：

```xml
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>https://example.com/sitemap-posts.xml</loc>
  </sitemap>
  <sitemap>
    <loc>https://example.com/sitemap-products.xml</loc>
  </sitemap>
</sitemapindex>
```

拆分后更容易定位问题：如果产品页 Sitemap 读取失败，不会影响文章页 Sitemap 的排查。

## 检查清单

- `https://example.com/robots.txt` 可以直接访问。
- Sitemap 中只包含可索引的规范 URL。
- `robots.txt` 没有误拦 CSS、JavaScript、图片等页面渲染资源。
- Search Console 的 Sitemap 报表能读取文件。
- 站内链接、canonical 和 Sitemap 指向同一套 URL 规则。

## 参考资料

- [构建并提交站点地图](https://developers.google.com/search/docs/crawling-indexing/sitemaps/build-sitemap)
- [robots.txt 简介](https://developers.google.com/search/docs/crawling-indexing/robots/intro)
- [robots meta 标记和 X-Robots-Tag](https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag)
