拦截搜索引擎爬虫
2025-07-02
19
参考资料
拦截搜索引擎爬虫
拦截搜索引擎爬虫操作示例
通过robots.txt文件
User-agent: * Disallow: /private/ Disallow: /temp/ Disallow: /admin/ User-agent: Googlebot Disallow: /search-results/
通过.htaccess文件(Apache)
# 禁止所有爬虫 SetEnvIfNoCase User-Agent .* bad_bot Deny from env=bad_bot # 禁止特定爬虫 SetEnvIfNoCase User-Agent "Googlebot" bad_bot SetEnvIfNoCase User-Agent "Bingbot" bad_bot Deny from env=bad_bot
通过Nginx配置
location / { if ($http_user_agent ~* (Googlebot|Bingbot|YandexBot)) { return 403; } }
通过PHP代码拦截
<?php $user_agent = $_SERVER['HTTP_USER_AGENT']; $bots = array('Googlebot', 'Bingbot', 'YandexBot', 'Slurp', 'DuckDuckBot'); foreach($bots as $bot) { if(stripos($user_agent, $bot) !== false) { header('HTTP/1.0 403 Forbidden'); exit; } } ?>
通过meta标签(页面级)
<meta name="robots" content="noindex, nofollow">
注意事项
robots.txt只是建议性拦截,不能真正阻止爬虫访问
服务器端拦截更可靠但可能影响SEO
拦截前应考虑对网站流量的潜在影响