1. 程式人生 > > robots.txt的語法和寫法詳解

robots.txt的語法和寫法詳解

html txt 訪問 isa 字符 包含 all 屏蔽 有道

robots.txt是一個純文本文件,是搜索引擎蜘蛛爬行網站的時候要訪問的第一個文件,當蜘蛛訪問一個站點時,它會首先檢查該站點根目錄下是否存在robots.txt,如果存在,搜索機器人就會按照該文件中的內容來確定訪問的範圍,相當於網站與搜索引蜘蛛遵循協議,如果該文件不存在,所有的搜索蜘蛛將能夠訪問網站上所有沒有被屏蔽的網頁,作為站長,我們就可以通過robots.txt文件屏蔽掉錯誤的頁面和一些不想讓蜘蛛爬取和收錄的頁面,那麽robots.txt該怎麽樣寫呢?

  robots的語法:
  1、User-agent 定義搜索引擎。一般情況下,網站裏面都是:User-agent: *,這裏*的意思是所有,表示定義所有的搜索引擎。比如,我想定義百度,那麽就是User-agent: Baiduspider;定義google,User-agent: Googlebot。
  2、Disallow 禁止爬取。如,我想禁止爬取我的admin文件夾,那就是Disallow: /admin/。禁止爬取admin文件夾下的login.html,
  Disallow: /admin/login.html。
  3、Allow 允許。我們都知道,在默認情況下,都是允許的。那為什麽還要允許這個語法呢?舉個例子:我想禁止admin文件夾下的所有文件,除了.html的網頁,那怎麽寫呢?我們知道可以用Disallow一個一個禁止,但那樣太費時間很精力了。這時候運用Allow就解決了復雜的問題,就這樣寫:
  Allow: /admin/.html$
  Disallow: /admin/。
  4、$ 結束符。例:Disallow: .php$ 這句話的意思是,屏蔽所有的以.php結尾的文件,不管前面有多長的URL,如abc/aa/bb//index.php也是屏蔽的。
  5、* 通配符符號0或多個任意字符。例:Disallow: *?* 這裏的意思是屏蔽所有帶“?”文件,也是屏蔽所有的動態URL。
  robots.txt文件寫法舉例說明
  禁止Google/百度等所有搜索引擎訪問整個網站
  User-agent: *
  Disallow: /
  允許所有的搜索引擎spider訪問整個網站(Disallow:可以用Allow: /替代)
  User-agent: *
  Disallow:
  禁止Baiduspider訪問您的網站,Google等其他搜索引擎不阻止
  User-agent: Baiduspider
  Disallow: /
  只允許Google spider: Googlebot訪問您的網站,禁止百度等其他搜索引擎
  User-agent: Googlebot
  Disallow:
  User-agent: *
  Disallow: /
  禁止搜索引擎蜘蛛spider訪問指定目錄
    (spider不訪問這幾個目錄。每個目錄要分開聲明,不能合在一起)
  User-agent: *
  Disallow: /cgi-bin/
  Disallow: /admin/
  Disallow: /~jjjj/
  禁止搜索引擎spider訪問指定目錄,但允許訪問該指定目錄的某個子目錄
  User-agent: *
  Allow: /admin/far
  Disallow: /admin/
  使用通配符星號"*"設置禁止訪問的url
     (禁止所有搜索引擎抓取/cgi-bin/目錄下的所有以".html"格式的網頁(包含子目錄))
  User-agent: *
  Disallow: /cgi-bin/*.html
  使用美元符號"$"設置禁止訪問某一後綴的文件
     (只允許訪問以".html"格式的網頁文件。)
  User-agent: *
  Allow: .html$
  Disallow: /
  阻止google、百度等所有搜索引擎訪問網站中所有帶有?的動態網址頁面
  User-agent: *
  Disallow: /*?*
  阻止Google spider:Googlebot訪問網站上某種格式的圖片
    (禁止訪問.jpg 格式的圖片)
  User-agent: Googlebot
  Disallow: .jpg$
  只允許Google spider:Googlebot抓取網頁和.gif格式圖片
     (Googlebot只能抓取gif格式的圖片和網頁,其他格式的圖片被禁止;
   其他搜索引擎未設置)
  User-agent: Googlebot
  Allow: .gif$
  Disallow: .jpg$
  .......
  只禁止Google spider:Googlebot抓取.jpg格式圖片
   (其他搜索引擎和其他格式圖片沒有禁止)
  User-agent: Googlebot
  Disallow: .jpg$
  聲明網站地圖sitemap
  這個告訴搜索引擎你的sitemap在哪,如:
  Sitemap: http://www.AAAA.com/sitemap.xml
  Google和百度對robots.txt文件的介紹: Google robotstxt,百度 robots.txt。
  PS:
  國內的搜索引擎蜘蛛
  百度蜘蛛:baiduspider
  搜狗蜘蛛:sogou spider
  有道蜘蛛:YodaoBot和OutfoxBot
  搜搜蜘蛛: Sosospider
  國外的搜索引擎蜘蛛
  google蜘蛛: googlebot
  yahoo蜘蛛:Yahoo! Slurp
  alexa蜘蛛:ia_archiver
  bing蜘蛛(MSN):msnbot

robots.txt的語法和寫法詳解