1.使用Robots meta標籤
使用robots meta標籤,就可以從網站中以網頁爲基礎排除基於HTML的內容。爲了用meta來排除頁面,只需簡單地將以下代碼插入到要排除的HTML文檔的<head>節
<meta name="robots" content="noindex,nofollow" />
這就表明頁面不能被索引,並且頁面的鏈接不能被跟隨。
2.robots.txt模式排除
meta標籤有技術限制,必須對應用程序源代碼修改,而且只能用HTML。
robots.txt是位於網站根目錄下的文本文件,並符合robots.txt標準。3個基本概念:
(1)只能有一個robots.txt
(2)robots.txt應放在web站點的根目錄下
(3)位於子目錄下的robots.txt無效果
下面位於站點根文件夾下的robots.txt文件示例,不允許任何robots(*)訪問站點的文件:
User-agent:*
Disallow:/
以下示例不允許任何以“/directory”開頭的URL地址被Google索引(googlebot是谷歌的用戶代理名):
User-agent:googlebot
Disallow:/directory
如果只希望directory文件夾下的內容被排除,/directory/
如果想要googlebot排除X,Y,Z,應該這樣寫:
User-agent:*
Disallow:X
Disallow:Y
User-agent:googlebot
Disallow:X
Disallow:Y
Disallow:Z
只允許google來檢索站點:
User-agent:googlebot
Disallow:
User-agent:*
Disallow:/
3.實時生成robots.txt
web開發人員可以實時動態地程序化生成robots.txt文件。在.htaccess中包括以下規則,能夠把robots.php映射成robots.txt,並使用腳本robots.php來生成後者。
.htaccess裏的以下規則,將對robots.txt的請求重寫到robots.php:
RewriteEngine On
RewriteRule ^robots.txt$ /robots.php
robots.php文件:
<?
header('Content-type:text/plain');
...
...
?>