
一、robots.txt文件它的作用是什么,為什么要寫?
robots.txt是用來告訴搜索引擎網(wǎng)站上哪些內(nèi)容可以被訪問、哪些不能被訪問。當(dāng)搜索引擎訪問一個網(wǎng)站的時候,它首先會檢查網(wǎng)站是否存在robots.txt,如果有則會根據(jù)文件命令訪問有權(quán)限的文件。
每個網(wǎng)站需要根據(jù)自身的要求寫好robots.txt文件,robots.txt文件長什么樣子?我們可以看看幾個網(wǎng)站的robots.txt文件。


而一個網(wǎng)站為什么要寫robots.txt,主要有四點(diǎn):
1、保護(hù)網(wǎng)站安全
2、節(jié)省流量
3、禁止搜索引擎收錄部分頁面
4、引導(dǎo)蜘蛛爬網(wǎng)站地圖
二、robots.txt的寫法與步驟
1、定義搜索引擎
用User-agent:來定義搜索引擎,其中*表示所有,Baiduspider表示蜘蛛,Googlebot表示谷歌蜘蛛。
也就是說User-agent:*表示定義所有蜘蛛,User-agent:Baiduspider表示定義蜘蛛。
常用搜索蜘蛛規(guī)則分解如下:
User-agent: Baiduspider
User-agent: Baiduspider-image
User-Agent: Googlebot
User-agent: Bingbot
User-Agent: 360Spider
User-Agent: Yisouspider
User-Agent: Sogouspider
User-agent: Sogou web spider
User-agent: Sogou inst spider
User-agent: Sogou spider2
User-agent: Sogou blog
User-agent: Sogou News Spider
User-agent: Sogou Orion spider
User-agent: ChinasoSpider
User-agent: Sosospider
User-Agent: Yahoo! Slurp
User-agent: MSNBot
User-agent: YoudaoBot
User-agent: EasouSpider
2、禁止與允許訪問
Disallow: /表示禁止訪問,Allow: /表示允許訪問。
在寫robots.txt時需特別注意的是,/前面有一個英文狀態(tài)下的空格(必須是英文狀態(tài)下的空格)。
3、禁止搜索引擎訪問網(wǎng)站中的某幾個文件夾,以a、b、c為例,寫法分別如下:
Disallow: /a/
Disallow: /b/
Disallow: /c/
4、禁止搜索引擎訪問文件夾中的某一類文件,以a文件夾中的js文件為例,寫法如下:
Disallow: /a/*.js
5、只允許某個搜索引擎訪問,以Baiduspider為例,寫法如下:
User-agent: Baiduspider
Disallow:
6、禁止訪問網(wǎng)站中的動態(tài)頁面
User-agent: *
Disallow: /*?*
7、只允許搜索引擎訪問某類文件,以htm為例,寫法如下:
User-agent: *
Allow: .htm$
Disallow: /
8、禁止某個搜索引擎抓取網(wǎng)站上的所有圖片,以Baiduspider為例,寫法如下:
User-agent: F
Disallow: .jpg$
Disallow: .jpeg$
Disallow: .gif$
Disallow: .png$
Disallow: .bmp$
三、robots.txt文件存放位置
robots.txt文件存放在網(wǎng)站根目錄下,并且文件名所有字母都必須小寫。
四、特別注意事項(xiàng)
在寫robots.txt文件時語法一定要用對,User-agent、Disallow、Allow、Sitemap這些詞都必須是 個字母大寫,后面的字母小寫,而且在:后面必須帶一個英文字符下的空格。
網(wǎng)站上線之前切記寫robots.txt文件禁止蜘蛛訪問網(wǎng)站,如果不會寫就先了解清楚寫法之后再寫,以免給網(wǎng)站收錄帶來不必要的麻煩。
robots.txt文件生效時間在幾天至一個月之間,站長自身無法控制。但是,站長可以在統(tǒng)計(jì)中查看網(wǎng)站robots.txt文件是否生效。