sitemap 与 robots
sitemap.xml 告诉搜索引擎网站上有哪些页面可以抓取,robots.txt 则告诉搜索引擎哪些页面不能抓取。两者配合使用,是 SEO 基础配置的核心。本节将介绍 sitemap.xml 的格式规范、robots.txt 的编写规则,以及 noindex/nofollow 在页面级别控制索引的方法。
前置知识
阅读本节前,建议先了解:结构化数据(JSON-LD)
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>2024-01-15</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https://example.com/products</loc>
<lastmod>2024-01-14</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://example.com/about</loc>
<lastmod>2024-01-01</lastmod>
<changefreq>monthly</changefreq>
<priority>0.5</priority>
</url>
</urlset>sitemap 标签说明
| 标签 | 必填 | 说明 |
|---|---|---|
<urlset> | 是 | 根元素,包含命名空间声明 |
<url> | 是 | 每个页面的信息 |
<loc> | 是 | 页面的完整 URL |
<lastmod> | 否 | 最后修改日期(YYYY-MM-DD) |
<changefreq> | 否 | 更新频率(可选,Google 已忽略) |
<priority> | 否 | 优先级 0.0-1.0(可选,Google 已忽略) |
多语言 sitemap
xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/zh-CN/about</loc>
<lastmod>2024-01-15</lastmod>
<xhtml:link rel="alternate" hreflang="zh-CN" href="https://example.com/zh-CN/about" />
<xhtml:link rel="alternate" hreflang="en" href="https://example.com/en/about" />
<xhtml:link rel="alternate" hreflang="x-default" href="https://example.com/en/about" />
</url>
</urlset>sitemap 索引
当网站页面超过 50,000 个时,使用 sitemap 索引文件:
xml
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://example.com/sitemap-pages.xml</loc>
<lastmod>2024-01-15</lastmod>
</sitemap>
<sitemap>
<loc>https://example.com/sitemap-products.xml</loc>
<lastmod>2024-01-14</lastmod>
</sitemap>
<sitemap>
<loc>https://example.com/sitemap-images.xml</loc>
<lastmod>2024-01-13</lastmod>
</sitemap>
</sitemapindex>HTML 中的 sitemap 声明
html
<head>
<!-- 在 head 中声明 sitemap 位置(可选) -->
<link rel="sitemap" type="application/xml" title="Sitemap" href="/sitemap.xml" />
</head>robots.txt
基本格式
# robots.txt 文件放在网站根目录
# https://example.com/robots.txt
# 允许所有搜索引擎访问所有内容
User-agent: *
Allow: /
# 禁止访问特定目录
User-agent: *
Disallow: /admin/
Disallow: /private/
Disallow: /tmp/
Disallow: /*.json$
# 允许访问特定文件
Allow: /admin/login.html
# sitemap 位置
Sitemap: https://example.com/sitemap.xmlrobots.txt 指令
| 指令 | 说明 |
|---|---|
User-agent | 目标搜索引擎爬虫 |
Allow | 允许访问的路径 |
Disallow | 禁止访问的路径 |
Sitemap | sitemap 文件的 URL |
常见 User-agent
| User-agent | 搜索引擎 |
|---|---|
Googlebot | |
Baiduspider | 百度 |
Bingbot | Bing |
YandexBot | Yandex |
DuckDuckBot | DuckDuckGo |
* | 所有爬虫 |
robots.txt 示例
# 仅允许 Google 和百度
User-agent: Googlebot
Allow: /
User-agent: Baiduspider
Allow: /
User-agent: *
Disallow: /
# 禁止特定文件类型
User-agent: *
Disallow: /*.pdf$
Disallow: /*.json$
# 禁止搜索参数页面
User-agent: *
Disallow: /search?q=*
Disallow: /*?sort=*
Disallow: /*?page=*
# 允许访问 CSS 和 JS(Google 建议允许)
User-agent: Googlebot
Allow: /css/
Allow: /js/
Allow: /fonts/noindex 和 nofollow
meta robots noindex
html
<!-- 不索引当前页面 -->
<meta name="robots" content="noindex" />
<!-- 不跟踪页面中的链接 -->
<meta name="robots" content="nofollow" />
<!-- 不索引也不跟踪 -->
<meta name="robots" content="noindex, nofollow" />链接级别 nofollow
html
<!-- 单个链接添加 nofollow -->
<a href="https://example.com/external" rel="nofollow">外部链接</a>
<!-- 多个指令 -->
<a href="https://example.com" rel="nofollow noopener noreferrer">链接</a>HTTP 头 noindex
# 通过 HTTP 响应头设置 noindex
X-Robots-Tag: noindex
# 针对特定搜索引擎
X-Robots-Tag: googlebot: noindex
# 组合指令
X-Robots-Tag: noindex, nofollow注意事项
- robots.txt 不等于 noindex:robots.txt 只是告诉爬虫不要抓取,页面仍可能被索引(如果有外部链接指向它)
- sitemap 必须可公开访问:确保搜索引擎能访问 sitemap.xml
- URL 必须完整:sitemap 中的 URL 必须包含协议(https://)
- 定期更新:sitemap 应与网站内容同步更新
- 测试 robots.txt:使用 Google Search Console 的 robots.txt 测试工具
最佳实践
- 在网站根目录放置 robots.txt
- 在 robots.txt 中声明 sitemap 位置
- 使用 sitemap.xml 列出所有需要索引的页面
- 需要保密的页面使用 noindex + robots.txt Disallow
- 外部链接使用 rel="nofollow"
- 定期使用 Google Search Console 提交和检查 sitemap
- 禁止抓取非必要的资源(JSON API、内部工具等)
下一节
继续学习:canonical URL