修改 Drupal 的 robots.txt 文件

bookmark
Drupal版本: 
6.x

在 SEO (Search Engine Optimization——搜索引擎优化) 时关键的一点是为搜索引擎的爬虫提供一个好的、干净的链接地址,这就意味着在一个网站内不能有内容重复的两个或以上不同链接地址的页面。 然后这个问题在对 Drupal SEO 时尤为突出。而且如果使用的第三方模块中存在一些不合理的链接,那么这个问题就会更加严重了。

这时我们就要用到 robots.txt 这个文件,它的作用是限制爬虫机器人抓取文件范围。从 Drupal 5 时代起,Drupal 安装包里就包含了这个文件了,它的位置在安装包的根目录下。但是它还不够完美,需要一些改善。

修改Drupal 的默认 robots.txt 文件

抓取延时

 第一步要做的就是注释掉 Crawl-delay 这行。除非您的网站规模很大,或者备受爬行机器人的困扰,如果不是这样,那么不需要这行。

Crawl-delay: 10

在 Robots.txt 中,下面提及到规则都可以帮助减少爬虫收取重复内容的数量。

目录

 下面这部分是在默认 robots.txt 文件中 Drupal 设置的禁止爬虫访问的实际存在的目录。 

# Directories
Disallow: /database/
Disallow: /includes/
Disallow: /misc/
Disallow: /modules/
Disallow: /sites/
Disallow: /themes/
Disallow: /scripts/
Disallow: /updates/
Disallow: /profiles/
这部分可以保持不变。只要记住由于默认设置中 Drupal 禁止爬虫抓取 /sites//modules/ 以及 /themes/ 这三个目录,所以爬虫抓取不到您网站的 logo 图片。但是如果您自定义了 logo 图片,记得把它放在其他文件夹内 (如:/files/ ),然后用一个关键字作为名字,重明名 logo 图片。

文件

下面这部分是 Drupal 的根目录下的默认文件。我重来没有看到这些文嘉被加入索引,但是如果你喜欢可以保留他们不变。但是不要删除 CHANGELOG.txt 这个文件,因为就算过了一段时间,它还是能提醒您 Drupal 的版本号。

# FilesDisallow: /xmlrpc.php
Disallow: /CHANGELOG.txt
Disallow: /cron.php
Disallow: /INSTALL.mysql.txt
Disallow: /INSTALL.pgsql.txt
Disallow: /install.php
Disallow: /INSTALL.txt
Disallow: /LICENSE.txt
Disallow: /MAINTAINERS.txt
Disallow: /update.php
Disallow: /UPGRADE.txt
Disallow: /xmlcpc.php

开启 Clean URLs 功能的路径

这是默认 robots.txt 文件中最重要的一部分,因为这里有些问题: 

# Paths (clean URLs)
Disallow: /admin/
Disallow: /comment/reply/
Disallow: /contact/
Disallow: /logout/
Disallow: /node/add/
Disallow: /search/
Disallow: /user/register/
Disallow: /user/password/
Disallow: /user/login/

在 Drupal 中,链接的尾部不包含斜线,所以您可以参照下面高亮显示的位置修改 Robots.txt 中的相应规则。 

# Paths (clean URLs)
Disallow: /admin/
Disallow: /comment/reply/
Disallow: /contact
Disallow: /logout
Disallow: /node/add
Disallow: /search/
Disallow: /user/register
Disallow: /user/password
Disallow: /user/login

举例来说,每个 node 页面上的“Login or register to post comments”(登录或注册后发表评论)链接地址形式如下:http://mypetal.com/user/login?destination=comment/reply/806%2523comment-formhttp://mypetal.com/user/register?destination=comment/reply/806%2523comment-form。在默认的 robots.txt 中的规则是不会屏蔽搜索引擎的爬虫抓取这些链接,但是如果您删除上面高亮显示部分末尾的斜线。 

没有开启 Clean URLs 功能的路径

下面这部分针对与没有开启 Clean URLs 模块下的规则,您可以参照上面开启 Clean URLs 模块的修改逻辑来修改下面这部分。

 

# Paths (no clean URLs)
Disallow: /?q=admin/
Disallow: /?q=aggregator/
Disallow: /?q=comment/reply/
Disallow: /?q=contact/
Disallow: /?q=logout/
Disallow: /?q=node/add/
Disallow: /?q=search/
Disallow: /?q=user/password/
Disallow: /?q=user/register/
Disallow: /?q=user/login/

如果您没有开启 Clean URLs 模块,并且提交到搜索引擎并生成索引,那么这种页面:http://example.com/?q=node/25 就会拥有 PageRank。如果您正打算开启 Clean URLs 模块,那么别忘了在 .htaccess 中设置301 redirects(重定向):把动态链接重定向到简介链接。

其他规则

我同时推荐您在仔细阅读并理解后添加下面的规则:

在网站中,每个模块都可能添加额外的链接,这样会增加大量的重复内容的页面,同时在搜索引擎抓取页面时增加服务器的负担。在这里为核心模块定制一些额外的规则。

Disallow: /node$

http://mypetal.com/node 页面和 http://mypetal.com/ 页面重复。

Disallow: /user$

添加上面这条规则可以阻止爬虫抓取 http://mypetal.com/node 页面。如果您想对搜索引擎屏蔽所有用 user 页面,只需删除最后的。

Disallow: /*sort=

如果安装论坛模块、视图模块(Views)或其他包含有按**排序(Sort by **)功能的模块,就需要以上这条规则。

Disallow: /search$

添加上面这条规则可以阻止爬虫抓取 http://mypetal.com/search 链接。而这个链接会进行一个302 redirect 重定向到 http://mypetal.com/search/node 这个页面,而这个页面已经被 Drupal 默认的 Robots.txt 规则屏蔽。

Disallow: [front page]  (请把 [front page] 改为您指定的首页)

如果您在 Home › Administer › Site configuration 页面改变了默认首页,这样可以对爬虫屏蔽这个链接。或者您可以选择使用 Global Redirect 模块,它会自动重地向到真正的首页页面。

文章说明

  • 原文地址:http://tips.webdesign10.com/robots-txt-and-drupal  (For Drupal 5)
  • Cityreader 对原文进行翻译,并针对 Drupal 6 版本做了相应改动,结合了自己的理解和注释。
  • 转载本文请留言,请保留作者名和网页地址:)