关于Robots.txt的语法

谈谈ROBOTS.TXT语法和做用php

咱们知道,搜索引擎都有本身的“搜索机器人”(ROBOTS),并经过这些ROBOTS在网络上沿着网页上的连接(通常是http和src连接)不断抓取资料创建本身的数据库。css


对于网站管理者和内容提供者来讲,有时候会有一些站点内容,不但愿被ROBOTS抓取而公开。为了解决这个问题,ROBOTS开发界提供了两个办法:一个是robots.txt,另外一个是The Robots META标签。 html

 

1、    robots.txtjava

一、 什么是robots.txt?
robots.txt是一个纯文本文件,经过在这个文件中声明该网站中不想被robots访问的部分,这样,该网站的部分或所有内容就能够不被搜索引擎收录了,或者指定搜索引擎只收录指定的内容。当一个搜索机器人访问一个站点时,它会首先检查该站点根目录下是否存在robots.txt,若是找到,搜索机器人就会按照该文件中的内容来肯定访问的范围,若是该文件不存在,那么搜索机器人就沿着连接抓取。web


robots.txt必须放置在一个站点的根目录下,并且文件名必须所有小写
网站 URL
相应的 robots.txt的 URL
http://www.jinpingmei.org.cn
http://www.jinpingmei.org.cn/robots.txtajax

http://www.w3.org/
http://www.w3.org/robots.txt数据库

http://www.w3.org:80/
http://www.w3.org:80/robots.txtapi

http://www.w3.org:1234/
http://www.w3.org:1234/robots.txt网络

http://w3.org/
http://w3.org/robots.txtide

二、robots 语法:

“robots.txt”文件包含一条或更多的记录,这些记录经过空行分开(以CR,CR/NL, or NL做为结束符),每一条记录的格式以下所示:
“<field>:<optionalspace><value><optionalspace>”。
在该文件中可使用#进行注解,具体使用方法和UNIX中的惯例同样。该文件中的记录一般以一行或多行User-agent开始,后面加上若干Disallow行,详细状况以下:
User-agent:
在”robots.txt”文件中,若是有多条User-agent记录说明有多个robot会受到”robots.txt”的限制,对该文件来讲,至少要有一条User-agent记录。若是该项的值设为*,则对任何robot均有效,在”robots.txt”文件中,”User-agent:*”这样的记录只能有一条。
若是在”robots.txt”文件中,加入”User-agent:SomeBot”和若干Disallow、Allow行,那么名为”SomeBot”只受到”User-agent:SomeBot”后面的Disallow和Allow行的限制。
Disallow:
这个值能够是一条完整的路径,也能够是路径的非空前缀,以Disallow项的值开头的URL不会被robot访问。例如”Disallow:/help”禁止robot访问/help.html、/helpabc.html、/help/index.html,而”Disallow:/help/”则容许robot访问/help.html、/helpabc.html,不能访问/help /index.html。
“Disallow:”说明容许robot访问该网站的全部url,在”/robots.txt”文件中,至少要有一条Disallow记录。若是”/robots.txt”不存在或者为空文件,则对于全部的搜索引擎robot,该网站都是开放的。
 要拦截整个网站,请使用正斜扛。
Disallow:/
 要拦截目录及其中的全部内容,请在目录名后添加正斜扛。
Disallow:/private_directory/
 要拦截网页,请列出该网页。
Disallow:/private_file.html
Allow:
与Disallow项类似,这个值能够是一条完整的路径,也能够是路径的前缀,以Allow项的值开头的URL是容许robot访问的。例如”Allow:/hibaidu”容许robot访问/hibaidu.htm、/hibaiducom.html、/hibaidu /com.html。一个网站的全部URL默认是Allow的,因此Allow一般与Disallow搭配使用,实现容许访问一部分网页同时禁止访问其它全部URL的功能。
注意:Disallow与Allow行的顺序是有意义的,robot会根据第一个匹配成功的Allow或Disallow行肯定是否访问某个URL。
使用 * 匹配字符序列
您可以使用星号 (*) 来匹配字符序列。例如,要拦截对全部以”专用”开头的子目录的访问,可以使用下列条目:
User-Agent:Googlebot
Disallow:/专用*/
要拦截对全部包含问号 (?) 的网址的访问,可以使用下列条目:
User-agent:*
Disallow:/*?
使用 $ 匹配网址的结束字符
您可以使用 $ 字符指定与网址的结束字符进行匹配。例如,要拦截以 .asp 结尾的网址,可以使用下列条目:
User-Agent:Googlebot
Disallow:/*.asp$
您可将此模式匹配与 Allow 指令配合使用。例如,若是 ? 表示一个会话 ID,您可排除全部包含该 ID 的网址,确保搜索引擎蜘蛛不会抓取重复的网页。可是,以 ? 结尾的网址多是您要包含的网页版本。在此状况下,可对 robots.txt 文件进行以下设置:
User-agent:*
Allow:/*?$
Disallow:/*?
Disallow:/ *? 一行将拦截包含 ? 的网址(具体而言,它将拦截全部以您的域名开头、后接任意字符串,而后是问号 (?),然后又是任意字符串的网址)。
Allow: /*?$ 一行将容许包含任何以 ? 结尾的网址(具体而言,它将容许包含全部以您的域名开头、后接任意字符串,而后是问号 (?),问号以后没有任何字符的网址)。

下面是一些robots.txt基本的用法:

l         禁止全部搜索引擎访问网站的任何部分:
User-agent: *
Disallow: /

l         容许全部的robot访问
User-agent: *
Disallow:
或者也能够建一个空文件 "/robots.txt" file

l         禁止全部搜索引擎访问网站的几个部分(下例中的cgi-bin、tmp、private目录)
User-agent: *
Disallow: /cgi-bin/
Disallow: /tmp/
Disallow: /private/

l         禁止某个搜索引擎的访问(下例中的BadBot)
User-agent: BadBot
Disallow: /

l         只容许某个搜索引擎的访问(下例中的WebCrawler)
User-agent: WebCrawler
Disallow:

User-agent: *
Disallow: /

三、  常见搜索引擎机器人Robots名字

名称                     搜索引擎

 

Baiduspider            http://www.baidu.com
Scooter              http://www.altavista.com
ia_archiver             http://www.alexa.com
Googlebot           http://www.google.com
FAST-WebCrawler http://www.alltheweb.com
Slurp                  http://www.inktomi.com
MSNBOT            http://search.msn.com


四、 robots.txt举例

下面是一些著名站点的robots.txt:
http://www.cnn.com/robots.txt
http://www.google.com/robots.txt
http://www.ibm.com/robots.txt
http://www.sun.com/robots.txt
http://www.eachnet.com/robots.txt

五、 常见robots.txt错误


l         颠倒了顺序:
错误写成
User-agent: *
Disallow: GoogleBot

正确的应该是:
User-agent: GoogleBot
Disallow: *

l         把多个禁止命令放在一行中:
例如,错误地写成
Disallow: /css/ /cgi-bin/ /p_w_picpaths/

正确的应该是
Disallow: /css/
Disallow: /cgi-bin/
Disallow: /p_w_picpaths/

l         行前有大量空格
例如写成
        Disallow: /cgi-bin/
尽管在标准没有谈到这个,可是这种方式很容易出问题。

l         404重定向到另一个页面:
当Robot访问不少没有设置robots.txt文件的站点时,会被自动404重定向到另一个Html页面。这时Robot经常会以处理robots.txt文件的方式处理这个Html页面文件。虽然通常这样没有什么问题,可是最好能放一个空白的robots.txt文件在站点根目录下。

l         采用大写。例如
USER-AGENT: EXCITE
DISALLOW:
虽然标准是没有大小写的,可是目录和文件名应该小写:
user-agent:GoogleBot
disallow:

l         语法中只有Disallow,没有Allow!
错误的写法是:
User-agent: Baiduspider      
Disallow: /john/
allow: /jane/

l         忘记了斜杠/
错误的写作:
User-agent: Baiduspider      
Disallow: css

正确的应该是
User-agent: Baiduspider      
Disallow: /css/ 

2、          Robots META标签


一、什么是Robots META标签


Robots.txt文件主要是限制整个站点或者目录的搜索引擎访问状况,而Robots META标签则主要是针对一个个具体的页面。和其余的META标签(如使用的语言、页面的描述、关键词等)同样,Robots META标签也是放在页面的中,专门用来告诉搜索引擎ROBOTS如何抓取该页的内容。
二、Robots META标签的写法:


Robots META标签中没有大小写之分name=”Robots”表示全部的搜索引擎,能够针对某个具体搜索引擎写为name=”BaiduSpider”。content部分有四个指令选项:index、noindex、follow、nofollow,指令间以“,”分隔。

INDEX 指令告诉搜索机器人抓取该页面;

FOLLOW 指令表示搜索机器人能够沿着该页面上的连接继续抓取下去;

Robots Meta标签的缺省值是INDEX和FOLLOW,只有inktomi除外,对于它,缺省值是INDEX,NOFOLLOW。

须要注意的是:上述的robots.txt和Robots META标签限制搜索引擎机器人(ROBOTS)抓取站点内容的办法只是一种规则,须要搜索引擎机器人的配合才行,并非每一个ROBOTS都遵照的。

目前看来,绝大多数的搜索引擎机器人都遵照robots.txt的规则,而对于Robots META标签,目前支持的并很少,可是正在逐渐增长,如著名搜索引擎GOOGLE就彻底支持,并且GOOGLE还增长了一个指令“archive”,能够限制GOOGLE是否保留网页快照。例如:

表示抓取该站点中页面并沿着页面中连接抓取,可是不在GOOLGE上保留该页面的网页快照


例子:
# robots, scram

User-agent: *
Disallow:   /cgi-bin
Disallow:   /TRANSCRIPTS
Disallow:   /development
Disallow:   /third
Disallow:   /beta
Disallow:   /java
Disallow:   /shockwave
Disallow:   /JOBS
Disallow:   /pr
Disallow:   /Interactive
Disallow:   /alt_index.html
Disallow:   /webmaster_logs
Disallow:   /newscenter
Disallow:       /virtual
Disallow:       /DIGEST
Disallow:       /QUICKNEWS
Disallow:       /SEARCH

User-agent: Mozilla/3.01 (hotwired-test/0.1)
Disallow:   /cgi-bin
Disallow:   /TRANSCRIPTS
Disallow:   /development
Disallow:   /third
Disallow:   /beta
Disallow:   /java
Disallow:   /shockwave
Disallow:   /JOBS
Disallow:   /pr
Disallow:   /Interactive
Disallow:   /alt_index.html
Disallow:   /webmaster_logs
Disallow:   /newscenter
Disallow:       /virtual
Disallow:       /DIGEST
Disallow:       /QUICKNEWS
Disallow:       /SEARCH

User-agent: Slurp
Disallow:   /cgi-bin
Disallow:   /TRANSCRIPTS
Disallow:   /development
Disallow:   /third
Disallow:   /beta
Disallow:   /java
Disallow:   /shockwave
Disallow:   /JOBS
Disallow:   /pr
Disallow:   /Interactive
Disallow:   /alt_index.html
Disallow:   /webmaster_logs
Disallow:   /newscenter
Disallow:       /virtual
Disallow:       /DIGEST
Disallow:       /QUICKNEWS
Disallow:       /SEARCH


如咱们DZ的程序 的写法,你们结合上面的写法说明 应该能看懂

#
# robots.txt for Discuz! Board
# Version 5.5.0
#

User-agent: *

Disallow: /admin/ Disallow: /api/ Disallow: /p_w_uploads/ Disallow: /customavatars/ Disallow: /p_w_picpaths/ Disallow: /forumdata/ Disallow: /include/ Disallow: /ipdata/ Disallow: /templates/ Disallow: /plugins/ Disallow: /mspace/ Disallow: /wap/ Disallow: admincp.php Disallow: ajax.php Disallow: digest.php Disallow: logging.php Disallow: member.php Disallow: memcp.php Disallow: misc.php Disallow: my.php Disallow: pm.php Disallow: post.php Disallow: register.php Disallow: rss.php Disallow: search.php Disallow: seccode.php Disallow: topicadmin.php

相关文章
相关标签/搜索