(一)Robots Exclusion Protocol协议简介
当Robot访问一个Web站点时,比如http://www.some.com/,它先去检查文件http://www.some.com/robots.txt。如果这个文件存在,它便会按照这样的记录格式去分析:
User-agent:*Disallow: /cgi-bin/Disallow: /tmp/Disallow: /~joe/
以确定它是否应该检索站点的文件。这些记录是专门给Web Robot看的,一般的浏览者大概永远不会看到这个文件。
在一个站点上只能有一个“/robots.txt”文件,而且文件名的每个字母要求全部是小写。在Robot的记录格式中每一个单独的“Disallow” 行表示你不希望Robot访问的URL,每个URL必须单独占一行,不能出现“Disallow: /cgi-bin/ /tmp/”这样的病句。同时在一个记录中不能出现空行,这是因为空行是多个记录分割的标志。
User-agent行指出的是Robot或其他代理的名称。在User-agent行,'*' 表示所有的Robot。
下面是几个robot.txt的例子:
在整个服务器上拒绝所有的robots:User-agent: *Disallow: /
允许所有的robots访问整个站点:User-agent: *Disallow: ;或者产生一个空的“/robots.txt”文件
服务器的部分内容允许所有的robot访问User-agent: *Disallow: /cgi-bin/Disallow: /tmp/Disallow: /private/
拒绝某一个专门的robot:User-agent: BadBotDisallow: /
只允许某一个Robot光顾:User-agent: WebCrawlerDisallow:User-agent: *Disallow: /
最后我们给出 http://www.w3.org/站点上的robots.txt:
# For use by search.w3.org
User-agent: W3Crobot/1
Disallow:
User-agent: *
Disallow: /Member/ # This is restricted to W3C Members only
Disallow: /member/ # This is restricted to W3C Members only
Disallow: /team/ # This is restricted to W3C Team only
Disallow: /TandS/Member # This is restricted to W3C Members only
Disallow: /TandS/Team # This is restricted to W3C Team only
Disallow: /Project
Disallow: /Systems
Disallow: /Web
Disallow: /Team
Robots META tag的格式为:
〈META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"〉
像其他的META tag一样,它应该放在HTML文件的HEAD区:
〈html〉
〈head〉
〈meta name="robots" content="noindex,nofollow"〉
〈meta name="description" content="This page ...."〉
〈title〉...〈/title〉
〈/head〉
〈body〉
...
Robots META tag指令用逗号隔开,可以使用的指令包括[NO]INDEX和[NO] FOLLOW。INDEX指令指出一个索引性Robot是否可以对本页进行索引,FOLLOW指令指出Robot是否可以跟踪本页的链接。缺省的情况是INDEX和FOLLOW。例如:
〈meta name="robots" content="index,follow"〉
〈meta name="robots" content="noindex,follow"〉
〈meta name="robots" content="index,nofollow"〉
〈meta name="robots" content="noindex,nofollow"〉
在制作和维护Web时,应当考虑对Web Robot程序的管理。
------------------------------------
建立一个robots.txt 文档,
如果你的网站是www.yingz.com,拒绝访问IMG目录则为:
在 robots.txt 写上以下代码
# For use by search.yingz.com
Disallow:
User-agent: *
Disallow: /img/
----------------------------------
转自:
http://blog.sina.com.cn/s/blog_4cd012fc010085mb.html
-----------------------------------------------------------------------------------------------------------------
(二)Robots Exclusion Protocol协议概述
一.什么是robots.txt文件?
搜索引擎通过一种程序robot(又称spider),自动访问互联网上的网页并获取网页信息。
您可以在您的网站中创建一个纯文本文件robots.txt,在这个文件中声明该网站中不想被robot访问的部分,这样,该网站的部分或全部内容就可以不被搜索引擎收录了,或者指定搜索引擎只收录指定的内容。
二. robots.txt文件放在哪里?
robots.txt文件应该放在网站根目录下。举例来说,当robots访问一个网站(比如http://www.abc.com)时,首先会检查该网站中是否存在http://www.abc.com/robots.txt这个文件,如果机器人找到这个文件,它就会根据这个文件的内容,来确定它访问权限的范围。
网站 URL | 相应的 robots.txt的 URL |
http://www.w3.org/ | http://www.w3.org/robots.txt |
http://www.w3.org:80/ | http://www.w3.org:80/robots.txt |
http://www.w3.org:1234/ | http://www.w3.org:1234/robots.txt |
http://w3.org/ | http://w3.org/robots.txt |
三. robots.txt文件的格式
"robots.txt"文件包含一条或更多的记录,这些记录通过空行分开(以CR,CR/NL, or NL作为结束符),每一条记录的格式如下所示:
"<field>:<optionalspace><value><optionalspace>"。
在该文件中可以使用#进行注解,具体使用方法和UNIX中的惯例一样。该文件中的记录通常以一行或多行User-agent开始,后面加上若干Disallow行,详细情况如下:
User-agent:
该项的值用于描述搜索引擎robot的名字,在"robots.txt"文件中,如果有多条User-agent记录说明有多个robot会受到该协议的限制,对该文件来说,至少要有一条User-agent记录。如果该项的值设为*,则该协议对任何机器人均有效,在"robots.txt"文件中,"User-agent:*"这样的记录只能有一条。Disallow :
该项的值用于描述不希望被访问到的一个URL,这个URL可以是一条完整的路径,也可以是部分的,任何以Disallow 开头的URL均不会被robot访问到。例如"Disallow: /help"对/help.html 和/help/index.html都不允许搜索引擎访问,而"Disallow: /help/"则允许robot访问/help.html,而不能访问/help/index.html。
任何一条Disallow记录为空,说明该网站的所有部分都允许被访问,在"/robots.txt"文件中,至少要有一条Disallow记录。如果"/robots.txt"是一个空文件,则对于所有的搜索引擎robot,该网站都是开放的。
四. robots.txt文件用法举例
例1. 禁止所有搜索引擎访问网站的任何部分 下载该robots.txt文件 | User-agent: * |
例2. 允许所有的robot访问 (或者也可以建一个空文件 "/robots.txt" file) |
User-agent: *
|
例3. 禁止某个搜索引擎的访问 | User-agent: BadBot |
例4. 允许某个搜索引擎的访问 | User-agent: baiduspider User-agent: * |
例5. 一个简单例子 | User-agent: * |
五. robots.txt文件参考资料
robots.txt文件的更具体设置,请参看以下链接:
· Web Server Administrator's Guide to the Robots Exclusion Protocol
· HTML Author's Guide to the Robots Exclusion Protocol
· The original 1994 protocol description, as currently deployed
· The revised Internet-Draft specification, which is not yet completed or implemented
转自:
http://hi.baidu.com/devil_1832/blog/item/bf98e91f75cfc805314e1531.html