• May 22, 2018

    if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot"))
    {
    // what to do
    }

    หรือ

    function _bot_detected() {
    
      return (
        isset($_SERVER['HTTP_USER_AGENT'])
        && preg_match('/bot|crawl|slurp|spider|mediapartners/i', $_SERVER['HTTP_USER_AGENT'])
      );
    }

    update 16-06-2017 https://support.google.com/webmasters/answer/1061943?hl=en

    หรือ

    $interestingCrawlers = array( 'google', 'yahoo' );
    $pattern = '/(' . implode('|', $interestingCrawlers) .')/';
    $matches = array();
    $numMatches = preg_match($pattern, strtolower($_SERVER['HTTP_USER_AGENT']), $matches, 'i');
    if($numMatches > 0) // Found a match
    {
    // $matches[1] contains an array of all text matches to either 'google' or 'yahoo'
    }

    หรือ

    You can checkout if it’s a search engine with this function :

    <?php
    function crawlerDetect($USER_AGENT)
    {
    $crawlers = array(
    'Google' => 'Google',
    'MSN' => 'msnbot',
          'Rambler' => 'Rambler',
          'Yahoo' => 'Yahoo',
          'AbachoBOT' => 'AbachoBOT',
          'accoona' => 'Accoona',
          'AcoiRobot' => 'AcoiRobot',
          'ASPSeek' => 'ASPSeek',
          'CrocCrawler' => 'CrocCrawler',
          'Dumbot' => 'Dumbot',
          'FAST-WebCrawler' => 'FAST-WebCrawler',
          'GeonaBot' => 'GeonaBot',
          'Gigabot' => 'Gigabot',
          'Lycos spider' => 'Lycos',
          'MSRBOT' => 'MSRBOT',
          'Altavista robot' => 'Scooter',
          'AltaVista robot' => 'Altavista',
          'ID-Search Bot' => 'IDBot',
          'eStyle Bot' => 'eStyle',
          'Scrubby robot' => 'Scrubby',
          'Facebook' => 'facebookexternalhit',
      );
      // to get crawlers string used in function uncomment it
      // it is better to save it in string than use implode every time
      // global $crawlers
       $crawlers_agents = implode('|',$crawlers);
      if (strpos($crawlers_agents, $USER_AGENT) === false)
          return false;
        else {
        return TRUE;
        }
    }
    ?>

    Then you can use it like :

    <?php $USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
      if(crawlerDetect($USER_AGENT)) return "no need to lang redirection";?>

    PHP function to detect spider bots

    https://99webtools.com/blog/php-function-to-detect-spider-bots/


    "Yahoo"=>"Slurp",
    "Bing"=>"bingbot"
    /*You can add more bot here*/
    );
    $regexp='/'. implode("|", $bots_list).'/';
    $ua=$_SERVER['HTTP_USER_AGENT'];
    if(preg_match($regexp, $ua,$matches))
    {
    $bot= array_search($matches[0], $bots_list);
    return $bot;
    }
    else
    {
    return false;
    }
    }
    ?>

    redirect bot

    From the answer in the purposed helpful link, remove the exclamation marks !:

    RewriteEngine On

    RewriteCond %{REMOTE_ADDR} ^(110\.174\.129\.147|203\.217\.17\.162)
    RewriteCond %{HTTP_USER_AGENT} (Googlebot|msnbot|Surp) [NC]
    RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301,NE]
    So in your .htaccess example, change this:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    To this:

    RewriteEngine On

    RewriteCond %{REMOTE_ADDR} ^(110\.174\.129\.147|203\.217\.17\.162)
    RewriteCond %{HTTP_USER_AGENT} (Googlebot|msnbot|Surp) [NC]
    RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301,NE]

    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]



เวอไนน์ไอคอร์ส

ประหยัดเวลากว่า 100 เท่า!






เวอไนน์เว็บไซต์⚡️
สร้างเว็บไซต์ ดูแลเว็บไซต์

Categories