简体   繁体   中英

Redirecting search engines?

Would this code work to redirect search engines?

<?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM