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


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