加载课程详情中...
加载课程详情中...
Sitemap、Robots.txt 和索引优化
良好的网站结构帮助:
XML Sitemap 是一个列出网站所有重要页面的文件,帮助搜索引擎发现和索引你的内容。
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/</loc>
<lastmod>2025-01-18</lastmod>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https://example.com/blog/seo-guide</loc>
<lastmod>2025-01-17</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
</urlset>
| 元素 | 必需 | 说明 |
|---|---|---|
| loc | 是 | 页面 URL |
| lastmod | 否 | 最后修改日期 |
| changefreq | 否 | 更新频率(Google 通常忽略) |
| priority | 否 | 页面优先级(Google 通常忽略) |
✅ 应该包含:
- 有价值的内容页面
- 产品页面
- 分类页面
❌ 不应包含:
- 重复内容页面
- 带有 noindex 的页面
- 404 页面
- 带参数的重复 URL
当页面更新时,更新 sitemap 中的 lastmod。
单个 sitemap 限制:
大型网站使用 sitemap 索引:
<?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>
<lastmod>2025-01-18</lastmod>
</sitemap>
<sitemap>
<loc>https://example.com/sitemap-products.xml</loc>
<lastmod>2025-01-17</lastmod>
</sitemap>
</sitemapindex>
大多数 CMS 和框架支持自动生成:
Robots.txt 是一个告诉搜索引擎爬虫哪些页面可以抓取、哪些不可以的文件。
位置:https://example.com/robots.txt
# 允许所有爬虫访问所有内容
User-agent: *
Allow: /
# 禁止所有爬虫访问 /private/
User-agent: *
Disallow: /private/
# 禁止特定爬虫
User-agent: BadBot
Disallow: /
# 声明 Sitemap 位置
Sitemap: https://example.com/sitemap.xml
| 指令 | 说明 |
|---|---|
| User-agent | 指定规则适用的爬虫 |
| Allow | 允许抓取的路径 |
| Disallow | 禁止抓取的路径 |
| Sitemap | Sitemap 文件位置 |
# 标准配置
User-agent: *
Disallow: /admin/
Disallow: /private/
Disallow: /api/
Disallow: /search?
Disallow: /*?sort=
Disallow: /*?filter=
# 允许所有图片
Allow: /images/
# Sitemap
Sitemap: https://example.com/sitemap.xml
⚠️ Robots.txt 是"请求"而非"命令"
- 礼貌的爬虫会遵守
- 恶意爬虫会忽略
- 页面仍然可以访问
如果需要保护敏感内容:
- 使用密码保护
- 服务器端访问控制
- 不要仅依赖 robots.txt
Disallow: /page/
→ 爬虫不会抓取
→ 但如果有外链指向,页面可能仍被索引(显示 URL,无描述)
如果要完全阻止索引,需要用 noindex 标签
使用 Google Search Console 的 Robots.txt 测试工具验证配置。
在 HTML 中控制单个页面的索引行为:
<!-- 允许索引和跟踪链接(默认) -->
<meta name="robots" content="index, follow">
<!-- 不索引但跟踪链接 -->
<meta name="robots" content="noindex, follow">
<!-- 索引但不跟踪链接 -->
<meta name="robots" content="index, nofollow">
<!-- 不索引也不跟踪 -->
<meta name="robots" content="noindex, nofollow">
| 指令 | 说明 |
|---|---|
| index | 允许索引(默认) |
| noindex | 不索引此页面 |
| follow | 跟踪页面上的链接(默认) |
| nofollow | 不跟踪链接 |
| noarchive | 不显示缓存版本 |
| nosnippet | 不显示摘要 |
应该用 noindex 的页面:
- 感谢页面
- 管理后台
- 重复内容页面
- 低价值页面(搜索结果、筛选页)
- 隐私政策(如果不想被搜索)
对于非 HTML 资源(PDF、图片),使用 HTTP 头:
X-Robots-Tag: noindex
当多个 URL 显示相同或相似内容时,canonical 标签告诉搜索引擎哪个是"官方"版本。
以下 URL 显示相同内容:
https://example.com/page
https://example.com/page/
https://example.com/page?ref=twitter
http://example.com/page
https://www.example.com/page
<head>
<link rel="canonical" href="https://example.com/page">
</head>
所有版本都应该指向同一个规范 URL。
| 问题 | 解决方案 |
|---|---|
| HTTP vs HTTPS | 统一使用 HTTPS,设置重定向 |
| www vs 非 www | 选择一个,设置重定向 |
| 尾部斜杠 | 保持一致,使用 canonical |
| 参数变体 | 使用 canonical 指向无参数版本 |
| 分页 | 每页有自己的 canonical |
| 类型 | 含义 | 用途 | 权重传递 |
|---|---|---|---|
| 301 | 永久重定向 | URL 永久更改 | 是 |
| 302 | 临时重定向 | 临时跳转 | 有限 |
# Nginx
location /old-page {
return 301 /new-page;
}
# 或使用 rewrite
rewrite ^/old-url$ /new-url permanent;
# Apache .htaccess
Redirect 301 /old-page /new-page
避免重定向链
❌ A → B → C → D
✅ A → D, B → D, C → D
避免重定向循环
❌ A → B → A
重定向到相关页面
更新内部链接:直接链接到新 URL
保留重要重定向:至少保留 1 年
网站结构与索引优化要点:
这些是技术 SEO 的基础,确保搜索引擎能正确访问和理解你的网站。
下一节,我们将学习结构化数据。
觉得这节课有帮助?
出海了吗
chuhailema.com
AI出海 · 技术分享 · 实战经验
扫码阅读全文
获取更多出海干货