本文介绍了利用服务端User-Agent识别搜索引擎爬虫并执行跳转的技术。通过JavaScript和PHP两种语言实现,分别检测来路或爬虫标识,对搜索引擎访问执行301永久重定向或直接跳转,而对普通用户则保持页面不变。文中提供了具体的代码片段供参考,并提示此类黑帽SEO手段存在风险。
User-Agent识别与跳转原理
在SEO优化领域,一种常见的技术手段是在服务端根据客户端浏览器的User-Agent信息进行判断,并据此执行后续操作。
网络上流传着一段JavaScript代码,用于判断网站访客的来源。如果访客来自搜索引擎,则执行跳转;如果是直接访问,则不做任何变化。这段代码源自网络,历史悠久,感谢原作者的贡献。
JavaScript实现示例
<script language="javascript">
var pattern = /google/gi;
var pattern1= /yahoo/gi;
var keyValue=escape(document.referrer);
if (pattern.exec(keyValue))
setTimeout(
"windows.location='https://www.jb51.net'",10*1000);
else if(pattern1.exec(keyValue))
setTimeout(
"window.location='https://www.jb51.net'",10*1000);
</script>
当检测到搜索引擎的User-Agent时,代码会执行301跳转。目前网络上许多欺骗友情链接的做法正是基于此原理(相关代码将在文末提供)。
除了跳转,还有诸如“乔页”等多种思路。本文仅展示PHP版本的代码实现。
声明:以下代码均整理自网络,先展示一个简单示例。该示例通过PHP的$_SERVER['HTTP_USER_AGENT']变量进行判断。
PHP识别爬虫代码
<?php
$tmp = $_SERVER['HTTP_USER_AGENT'];
if(strpos($tmp, 'Googlebot') !== false){
echo '谷歌';
} else if(strpos($tmp, 'Baiduspider') >0){
echo '百度';
} else if(strpos($tmp, 'Yahoo! Slurp') !== false){
echo '雅虎';
} else if(strpos($tmp, 'msnbot') !== false){
echo 'Msn';
} else if(strpos($tmp, 'Sosospider') !== false){
echo '搜搜';
} else if(strpos($tmp, 'YodaoBot') !== false || strpos($tmp, 'OutfoxBot') !== false){
echo '有道';
} else if(strpos($tmp, 'Sogou web spider') !== false || strpos($tmp, 'Sogou Orion spider') !== false){
echo '搜狗';
} else if(strpos($tmp, 'fast-webcrawler') !== false){
echo 'Alltheweb';
} else if(strpos($tmp, 'Gaisbot') !== false){
echo 'Gais';
} else if(strpos($tmp, 'ia_archiver') !== false){
echo 'Alexa';
} else if(strpos($tmp, 'altavista') !== false){
echo 'AltaVista';
} else if(strpos($tmp, 'lycos_spider') !== false){
echo 'Lycos';
} else if(strpos($tmp, 'Inktomi slurp') !== false){
echo 'Inktomi';
}
?>
接下来展示带有跳转功能的代码段。
PHP执行跳转逻辑
<?php
$flag = false;
$tmp = $_SERVER['HTTP_USER_AGENT'];
if(strpos($tmp, 'Googlebot') !== false){
$flag = true;
} else if(strpos($tmp, 'Baiduspider') >0){
$flag = true;
} else if(strpos($tmp, 'Yahoo! Slurp') !== false){
$flag = true;
} else if(strpos($tmp, 'msnbot') !== false){
$flag = true;
} else if(strpos($tmp, 'Sosospider') !== false){
$flag = true;
} else if(strpos($tmp, 'YodaoBot') !== false || strpos($tmp, 'OutfoxBot') !== false){
$flag = true;
} else if(strpos($tmp, 'Sogou web spider') !== false || strpos($tmp, 'Sogou Orion spider') !== false){
$flag = true;
} else if(strpos($tmp, 'fast-webcrawler') !== false){
$flag = true;
} else if(strpos($tmp, 'Gaisbot') !== false){
$flag = true;
} else if(strpos($tmp, 'ia_archiver') !== false){
$flag = true;
} else if(strpos($tmp, 'altavista') !== false){
$flag = true;
} else if(strpos($tmp, 'lycos_spider') !== false){
$flag = true;
} else if(strpos($tmp, 'Inktomi slurp') !== false){
$flag = true;
}
if($flag == false){
header("Location: https://www.jb51.net" . $_SERVER['REQUEST_URI']);
// 自动转到https://www.jb51.net 对应的网页
// $_SERVER['REQUEST_URI'] 为域名后面的路径
// 或 换成 header("Location: https://www.jb51.net/abc/d.php");
exit();
}
?>
第三段代码实现了判断后执行301跳转的功能。
PHP 301跳转实现
if (preg_match(“#(google|slurp@inktomi|yahoo! slurp|msnbot)#si”, $_SERVER['HTTP_USER_AGENT'])) {
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: http://www.saoyu.com/”);
exit;
}}
黑帽SEO手段存在风险,请谨慎使用。如果网站中招,建议排查后门。