一:什么是Robots.txt?
robots.txt是一个协议,不是一个命令。robots.txt是搜索引擎访问网站时查看的第一个文件。robots.txt告诉搜索引擎蜘蛛,在这个网站上,有哪些页面是可以被查看收录,哪些是不可以的,亦或者是整站都不允许被抓取。
robots.txt文件两条规则:
Disallow:代表要拦截的网页
Allow:允许抓取的网页
无论是什么网站都建议设置一个robots.txt文件,就算网站的所有页面都允许蜘蛛访问也尽量设置。
robots.txt必须放置在一个网站的根目录下,文件名必须是小写。
robots.txt里面的内容大小写不可更改,Disallow等命令后面的冒号必须为英文状态下的冒号。
二:Robots.txt 文件应放在哪里?
Robots.txt 文件必须放在网站的根目录。放在子目录的 Robots.txt 文件搜索引擎不能爬取到,所以不会起任何作用。
例子:
https://yunyingwhy.com/robots.txt 有效
https://yunyingwhy.com/seo/robots.txt 无效
三:Robots.txt的使用方法
关于robots.txt一般站长需要注意以下几点:
如果你的站点对所有搜索引擎公开,则不用做这个文件或者robots.txt为空就行。
必须命名为:robots.txt,都是小写,robot后面加"s"。
robots.txt必须放置在一个站点的根目录下。如:通过https://yunyingwhy.com/robots.txt 可以成功访问到,则说明本站的放置正确。
一般情况下,robots.txt里只写着两个函数:User-agent和 Disallow。
观察这个页面并修改为自己的:https://yunyingwhy.com/robots.txt
有几个禁止,就得有几个Disallow函数,并分行描述。
至少要有一个Disallow函数,如果都允许收录,则写: Disallow: ,如果都不允许收录,则写:Disallow: / (注:只是差一个斜杆)。
补充说明:
User-agent: * 星号说明允许所有搜索引擎收录;
Disallow: /search.html 说明 https://yunyingwhy.com/search.html 这个页面禁止搜索引擎抓取。
Disallow: /index.php? 说明类似这样的页面https://yunyingwhy.com/index.php?search=owiq8eh&searchcategory=%25 禁止搜索引擎抓取。
四:robots.txt文件用法举例:
1. 允许所有的robot访问
User-agent: * Allow: / 或者 User-agent: * Disallow:
2. 禁止所有搜索引擎访问网站的任何部分
User-agent: *
Disallow: /
3. 仅禁止Baiduspider访问您的网站
User-agent: Baiduspider
Disallow: /
4. 仅允许Baiduspider访问您的网站
User-agent: Baiduspider
Disallow:
5. 禁止spider访问特定目录
User-agent: *
Disallow: /cgi-bin/
Disallow: /tmp/
Disallow: /~joe/
6. 允许访问特定目录中的部分url
User-agent: *
Allow: /cgi-bin/see
Allow: /tmp/hi
Allow: /~joe/look
Disallow: /cgi-bin/
Disallow: /tmp/
Disallow: /~joe/
7. 使用”*”限制访问url
禁止访问/cgi-bin/目录下的所有以”.htm”为后缀的URL(包含子目录)。
User-agent: *
Disallow: /cgi-bin/*.htm
8. 使用”$”限制访问url
仅允许访问以”.htm”为后缀的URL。
User-agent: *
Allow: .htm$
Disallow: /
9. 禁止访问网站中所有的动态页面
User-agent: *
Disallow: /*?*
10. 禁止Baiduspider抓取网站上所有图片
仅允许抓取网页,禁止抓取任何图片。
User-agent: Baiduspider
Disallow: .jpg$
Disallow: .jpeg$
Disallow: .gif$
Disallow: .png$
Disallow: .bmp$
11. 仅允许Baiduspider抓取网页和.gif格式图片
允许抓取网页和gif格式图片,不允许抓取其他格式图片
User-agent: Baiduspider
Allow: .gif$
Disallow: .jpg$
Disallow: .jpeg$
Disallow: .png$
Disallow: .bmp$
12. 仅禁止Baiduspider抓取.jpg格式图片
User-agent: Baiduspider
Disallow: .jpg$
标签: SEO整站优化