通過給網站設置適當的 robots.txt 對 Google 和百度 seo 優化的作用是很明顯的。 WordPress 博客網站也一樣。

我們先看看 robots.txt 是什麼,有什麼作用?

robots.txt 是什麼?

我們都知道 txt 後綴的文件是純文本文檔,robots 是機器人的意思,所以顧名思義,robots.txt 文件也就是給搜索引擎蜘蛛這個機器人看 的純文本文件。 robots.txt 是搜索引擎公認遵循的一個規範文 檔,它告訴 Google 、百度等搜索引擎哪些網頁允許抓取、索引並在搜索結果中顯示,哪些網頁是被禁止收錄的。 搜索引擎蜘蛛 spider(Googlebot/Baiduspider) 來訪問你的網站頁面的時候,首先會查看你的網站根目錄下是否有 robots.txt 文件,如 果有則按照裏面設置的規則權限對你網站頁面進行抓取和索引。如淘寶網就通過設置 robots.txt 屏蔽百度搜索引擎:

User-agent: Baiduspider
Disallow: /
User-agent: baiduspider
Disallow: /

robots.txt 的作用

我們瞭解了什麼是 robots.txt,那它有什麼作用,總體來説,robots.txt 文件至少有下面兩方面的作用:

1 、通過設置屏蔽搜索引擎訪問不必要被收錄的網站頁面,可以大大減少因 spider 抓取頁面所佔用的網站帶寬,小網站不明顯,大型網站就很明顯了。

2 、設置 robots.txt 可以指定 google 或百度不去索引哪些網址,比如我們通過 url 重寫將動態網址靜態化為永久固定鏈接之後,就可以通過 robots.txt 設置權限,阻止 Google 或百度等搜索引擎索引那些動態網址,從而大大減少了網站重複頁面,對 SEO 優化起到了很明顯的作用。

robots.txt 的寫法

關於如何寫 robots.txt 文件,在下面我們會以 WordPress 博客來作更具體舉例説明。這裏先提示幾點 robots.txt 寫法中應該注意的地方。如 robots.txt 文件裏寫入以下代碼:

User-agent: *
Disallow:
Allow: /

robots.txt 必須上傳到你的網站根名錄下,在子目錄下無效;

robots.txt,Disallow 等必須注意大小寫,不能變化;

User-agent,Disallow 等後面的冒號必須是英文狀態下的,冒號後面可以空一格,也可以不空格。網上有人説冒號後面必須有空格,其實沒有也是可以的,請看谷歌中文網站管理員博客的設置就是這樣:http://www.googlechinawebmaster.com/robots.txt ;

User-agent 表示搜索引擎 spider:星號 「*」 代表所有 spider,Google 的 spider 是 「Googlebot」,百度是 「Baiduspider」;

Disallow: 表示不允許搜索引擎訪問和索引的目錄;

Allow: 指明允許 spider 訪問和索引的目錄,Allow: / 表示允許所有,和 Disallow: 等效。

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$

Google 和百度對 robots.txt 文件的介紹: Google robotstxt,百度 robots.txt 。