经常在项目的根目录会看到一个robots.txt文件,今天就来普及一下它的作用
- 介绍
robots.txt 文件规定了搜索引擎抓取工具可以访问您网站上的哪些网址。 此文件
主要用于避免您的网站收到过多请求;它并不是一种阻止 Google 抓取某个网页的
机制。若想阻止 Google 访问某个网页,请使用 noindex 禁止将其编入索引,或
使用密码保护该网页。
- 用途
robots.txt 文件主要用于管理流向您网站的抓取工具流量
- 编写
robots.txt 文件应位于网站的根目录下。因此,对于网站 www.example.com,robots.txt 文件
的路径应为 www.example.com/robots.txt
下面是一个包含两条规则的简单 robots.txt 文件:
User-agent: Googlebot
Disallow: /nogooglebot/
User-agent: *
Allow: /
Sitemap: https://www.example.com/sitemap.xml
实际使用:
User-agent: *
Allow: /
Sitemap: https://www.example.com/sitemap.xml
- 更新 robots.txt 文件
实际使用的时候,站点内容可能会不停的更新,所以可以通过定时任务,定期修改文件内容
参考(https://developers.google.com/search/docs/crawling-indexing/robots/create-robots-txt?hl=zh-cn)
