Would this code work to redirect search engines?
&l开发者_开发问答t;?php
function check_if_spider()
{
$spiders = array(
'Googlebot', 'Yammybot', 'Openbot', 'Yahoo', 'Slurp', 'msnbot',
'ia_archiver', 'Lycos', 'Scooter', 'AltaVista', 'Teoma', 'Gigabot',
'Googlebot-Mobile'
);
foreach ($spiders as $spider)
{
if (eregi($spider, $_SERVER['HTTP_USER_AGENT']))
{
return TRUE;
}
}
return FALSE;
}
if (check_if_spider() == 1){
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: http://www.site.com');
exit();
}
?>
Yes, it would. But the function above is pretty non-optimal (it uses deprecated eregi() instead of simple string function strpos()). Also be careful: spider bots don't like situation when you display them content which is different from content for user browsers.
Here is what I think:
ergei is deprecated: From PHP manual:
This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.
Your function
check_if_spider()
returns TRUE/FALSE but you're checking for 1 outside, not very intuitive and readable code.It is much better to handle search engine bots in
robots.txt
or else via mod_rewrite rules in your .htaccess/config rather than inside PHP code.
精彩评论