robots.txt 文件详细说明

163 阅读2分钟

参考资料

  1. Bing 管理员工具
  2. 如何检查网站是否被百度收录?
  3. robots.txt 禁止所有
  4. robots.txt文件
  5. robots.txt 在哪里
  6. 百度收录资源平台
  7. robots.txt sitemap
  8. 360站长平台

robots.txt 文件详细说明

robots.txt 文件详细说明

定义

robots.txt 是一个文本文件,位于网站的根目录下,用于指导搜索引擎蜘蛛(爬虫)如何抓取网站的页面。它通过指定允许或禁止抓取的路径,控制搜索引擎对网站内容的访问。

语法
  • User-agent: 指定适用的爬虫名称。* 表示所有爬虫。

  • Disallow: 禁止抓取的路径。

  • Allow: 允许抓取的路径(通常用于在禁止的路径中例外允许某些内容)。

  • Sitemap: 指定网站地图的位置。

示例
  1. 允许所有爬虫访问所有内容

    User-agent: *Disallow:
    
  2. 禁止所有爬虫访问所有内容

    User-agent: *Disallow: /
    
  3. 禁止特定爬虫访问特定目录

    User-agent: GooglebotDisallow: /private/
    
  4. 允许特定爬虫访问特定目录,禁止其他爬虫

    User-agent: BingbotAllow: /public/ User-agent: *Disallow: /
    
  5. 指定网站地图

    Sitemap: https://www.example.com/sitemap.xml
    
注意事项
  • robots.txt 是公开的,任何人都可以查看,因此不应将其用于隐藏敏感信息。

  • robots.txt 中的路径区分大小写。

  • 尽管 robots.txt 可以指导爬虫,但它并不强制执行,爬虫可以选择忽略这些指令。

  • 使用 robots.txt 来阻止爬虫抓取页面可能会影响搜索引擎索引,进而影响网站的可见性。

案例

案例1:电商网站

一个电商网站希望禁止所有爬虫抓取 /cart//checkout/ 目录,但允许抓取其他所有内容。

User-agent: *Disallow: /cart/Disallow: /checkout/
案例2:新闻网站

一个新闻网站希望允许 Googlebot 和 Bingbot 抓取所有内容,但禁止其他爬虫抓取 /archives/ 目录。

User-agent: GooglebotAllow: / User-agent: BingbotAllow: / User-agent: *Disallow: /archives/
案例3:个人博客

一个个人博客希望禁止所有爬虫抓取 /admin//private/ 目录,并提供一个网站地图。

User-agent: *Disallow: /admin/Disallow: /private/ Sitemap: https://www.myblog.com/sitemap.xml

这些案例展示了如何根据不同需求配置 robots.txt 文件,以控制搜索引擎对网站内容的抓取行为。