为什么需要robots.txt和sitemap.xml?
很多新手站长会忽视这两个文件,结果搜索引擎要么抓不到页面,要么抓了一堆不该抓的内容(后台、缓存文件)。这两个文件是SEO的基础配置,却常被忽略。
nxh20110925:nxh20110925
添加微信好友, 获取更多信息
复制nxh20110925
添加微信好友, 获取更多信息
复制nxh20110925
一、robots.txt是什么?
robots.txt是放在网站根目录的文本文件,告诉搜索引擎爬虫:
- 哪些页面可以抓
- 哪些页面不能抓
- 网站地图在哪里
它是爬虫访问网站时第一个查看的文件。
二、个人博客推荐robots.txt配置
User-agent: * Allow: / Disallow: /zb_system/ Disallow: /zb_users/data/ Disallow: /zb_users/cache/ Disallow: /zb_users/plugin/ Sitemap: https://www.你的域名.com/sitemap.xml
说明:
Allow: /允许抓取所有公开内容。Disallow: /zb_system/禁止抓取后台。Disallow: /zb_users/data/禁止抓取用户数据。Sitemap告诉爬虫sitemap的位置。
三、robots.txt的常见错误
- 禁止抓取整个网站:写成
Disallow: /,结果整站不被收录。 - 路径写错:如
Disallow: /zb_system(少了斜杠),导致规则失效。 - 忘记写Sitemap:搜索引擎不会主动发现你的sitemap。
- 大小写错误:必须是
robots.txt,不能是Robots.txt。
四、sitemap.xml是什么?
sitemap.xml是网站地图,列出网站所有重要页面的URL、更新频率、优先级。帮助搜索引擎更高效地抓取网站内容,尤其是新网站和深层页面。
五、sitemap.xml的标准格式
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/</loc>
<lastmod>2026-09-14</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https://www.example.com/post/1.html</loc>
<lastmod>2026-09-14</lastmod>
<changefreq>weekly</changefreq>
<priority>0.6</priority>
</url>
</urlset>
字段说明:
- loc:页面URL(必填)。
- lastmod:最后修改时间(YYYY-MM-DD)。
- changefreq:更新频率(always/hourly/daily/weekly/monthly/yearly/never)。
- priority:相对优先级(0.0-1.0,1.0为最高)。
六、sitemap的大小限制
- 单个sitemap最多50000条URL。
- 单个sitemap文件不超过50MB。
- 超过限制需要使用sitemap索引文件(sitemapindex)。
七、如何提交sitemap给搜索引擎?
百度:
- 登录百度站长平台(ziyuan.baidu.com)。
- 添加并验证网站。
- 左侧"链接提交" → "sitemap" → 提交sitemap.xml的URL。
谷歌:
- 登录Google Search Console。
- 添加并验证网站。
- 左侧"索引" → "站点地图" → 输入sitemap.xml。
八、Z-Blog自动生成sitemap
Z-Blog有几款主流sitemap插件:
- STACMS:综合插件,自带sitemap生成。
- ZBlog Sitemap Generator:专门生成sitemap。
- 百度sitemap提交插件:除了生成还会自动推送给百度。
推荐使用"百度sitemap提交插件",可以一键提交新文章给百度,加速收录。
九、验证robots.txt和sitemap.xml是否生效
验证robots.txt:
- 访问
https://你的域名.com/robots.txt看是否正常返回。 - 百度站长平台 → "Robots"工具 → 检测你的robots.txt是否合法。
验证sitemap.xml:
- 访问
https://你的域名.com/sitemap.xml看是否正常返回。 - 百度站长平台 → "sitemap" → 查看"抓取诊断"结果。
- 谷歌Search Console → "站点地图" → 查看处理状态。
写在最后
robots.txt和sitemap.xml是SEO的"门面",花10分钟配置好这两个文件,能让搜索引擎更好地理解和抓取你的网站,是性价比最高的SEO优化之一。
关于作者
我是追忆,一个人摸索建站和 SEO 的个人站长。如果你也卡在建站路上,欢迎加我微信 nxh20110925(备注「博客」),一起交流。
还没有评论,来说两句吧...