RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1368277
Accepted
Arcadiy
Arcadiy
Asked:2022-06-04 03:57:47 +0000 UTC2022-06-04 03:57:47 +0000 UTC 2022-06-04 03:57:47 +0000 UTC

如何实现按国家和 Yandex 和 Google PS 机器人进行站点重定向?

  • 772

网站有 3 页

  1. 网站.ru
  2. site.ru/ru/
  3. site.ru/en/

进入第一页时,根据位置确定并重定向用户的国家/地区:在主页上,在最顶部,编写代码

<?php 
require_once('config.php'); //The above code
$geoplugin = new geoPlugin();
$geoplugin->locate();
// create a variable for the country code
$var_country_code = $geoplugin->countryCode;
// redirect based on country code:
if ($var_country_code == "RU") {
header('Location: https://www.site.ru/ru/');
}
else if ($var_country_code == "US") {
header('Location: https://www.site.ru/en/');
}
else if ($var_country_code == "UA") {
header('Location: https://www.site.ru/ru/');
}
else if ($var_country_code == "KG") {
header('Location: https://www.site.ru/ru/');
}
else if ($var_country_code == "UZ") {
header('Location: https://www.site.ru/ru/');
}
else {
header('Location: https://www.site.ru/en/');
}
?>

这是 config.php 文件的内容

<?php
   class geoPlugin {
    var $host = 'http://www.geoplugin.net/php.gp?ip={IP}&base_currency={CURRENCY}&lang={LANG}';
    var $currency = 'USD';      
    var $lang = 'en';    
    var $ip = null;
    var $city = null;
    var $region = null;
    var $regionCode = null;
    var $regionName = null;
    var $dmaCode = null;
    var $countryCode = null;
    var $countryName = null;
    var $inEU = null;
    var $euVATrate = false;
    var $continentCode = null;
    var $continentName = null;
    var $latitude = null;
    var $longitude = null;
    var $locationAccuracyRadius = null;
    var $timezone = null;
    var $currencyCode = null;
    var $currencySymbol = null;
    var $currencyConverter = null;      
    function __construct() {    
    }
    
    function locate($ip = null) {           
        global $_SERVER;            
        if ( is_null( $ip ) ) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }           
        $host = str_replace( '{IP}', $ip, $this->host );
        $host = str_replace( '{CURRENCY}', $this->currency, $host );
        $host = str_replace( '{LANG}', $this->lang, $host );            
        $data = array();            
        $response = $this->fetch($host);            
        $data = unserialize($response);         
        //set the geoPlugin vars
        $this->ip = $ip;
        $this->city = $data['geoplugin_city'];
        $this->region = $data['geoplugin_region'];
        $this->regionCode = $data['geoplugin_regionCode'];
        $this->regionName = $data['geoplugin_regionName'];
        $this->dmaCode = $data['geoplugin_dmaCode'];
        $this->countryCode = $data['geoplugin_countryCode'];
        $this->countryName = $data['geoplugin_countryName'];
        $this->inEU = $data['geoplugin_inEU'];
        $this->euVATrate = $data['euVATrate'];
        $this->continentCode = $data['geoplugin_continentCode'];
        $this->continentName = $data['geoplugin_continentName'];
        $this->latitude = $data['geoplugin_latitude'];
        $this->longitude = $data['geoplugin_longitude'];
        $this->locationAccuracyRadius = $data['geoplugin_locationAccuracyRadius'];
        $this->timezone = $data['geoplugin_timezone'];
        $this->currencyCode = $data['geoplugin_currencyCode'];
        $this->currencySymbol = $data['geoplugin_currencySymbol'];
        $this->currencyConverter = $data['geoplugin_currencyConverter'];            
    }       
    function fetch($host) {    
        if ( function_exists('curl_init') ) {                           
            //use cURL to fetch data
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $host);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.1');
            $response = curl_exec($ch);
            curl_close ($ch);               
        } else if ( ini_get('allow_url_fopen') ) {              
            //fall back to fopen()
            $response = file_get_contents($host, 'r');              
        } else {    
            trigger_error ('geoPlugin class Error: Cannot retrieve data. Either compile PHP with cURL support or enable allow_url_fopen in php.ini ', E_USER_ERROR);
            return;         
        }           
        return $response;
    }       
    function convert($amount, $float=2, $symbol=true) {         
        //easily convert amounts to geolocated currency.
        if ( !is_numeric($this->currencyConverter) || $this->currencyConverter == 0 ) {
            trigger_error('geoPlugin class Notice: currencyConverter has no value.', E_USER_NOTICE);
            return $amount;
        }
        if ( !is_numeric($amount) ) {
            trigger_error ('geoPlugin class Warning: The amount passed to geoPlugin::convert is not numeric.', E_USER_WARNING);
            return $amount;
        }
        if ( $symbol === true ) {
            return $this->currencySymbol . round( ($amount * $this->currencyConverter), $float );
        } else {
            return round( ($amount * $this->currencyConverter), $float );
        }
    }       
    function nearby($radius=10, $limit=null) {    
        if ( !is_numeric($this->latitude) || !is_numeric($this->longitude) ) {
            trigger_error ('geoPlugin class Warning: Incorrect latitude or longitude values.', E_USER_NOTICE);
            return array( array() );
        }           
        $host = "http://www.geoplugin.net/extras/nearby.gp?lat=" . $this->latitude . "&long=" . $this->longitude . "&radius={$radius}";         
        if ( is_numeric($limit) )
            $host .= "&limit={$limit}";             
        return unserialize( $this->fetch($host) );    
    }       
}    
?>

问题是它робот Гугла获得了网站的英文版本,而我拥有的主要版本是俄文。Робот Гула有美国人IP。是否可以通过 UserAgent 以某种方式编写条件,以便 Google 和 Yandex 机器人始终接收俄罗斯版本的网站?

以下是 Yandex 支持人员所写的内容:

我只想指出,我们的索引机器人可以从不同的页面访问,因此您应该确保在他们的请求上不断地执行相同的重定向,即使机器人从例如美国 IP 访问。您可以在我们的帮助页面上了解如何检查机器人是否属于 Yandex 。 并帮助用户代理

.

那正确吗?

<?php 
require_once('config.php'); //The above code
$geoplugin = new geoPlugin();
$geoplugin->locate();
// create a variable for the country code
$var_country_code = $geoplugin->countryCode;
// redirect based on country code:
if ($var_country_code == "RU") {
header('Location: https://www.site.ru/ru/');
}
else if ($var_country_code == "US") {
header('Location: https://www.site.ru/en/');
}
else if ($var_country_code == "UA") {
header('Location: https://www.site.ru/ru/');
}
else if ($var_country_code == "KG") {
header('Location: https://www.site.ru/ru/');
}
else if ($var_country_code == "UZ") {
header('Location: https://www.site.ru/ru/');
}
else {
header('Location: https://www.site.ru/en/');
}
if (!empty($_SERVER['HTTP_USER_AGENT'])) {
        $options = array(
            'YandexBot', 'YandexAccessibilityBot', 'YandexMobileBot','YandexDirectDyn',
            'YandexScreenshotBot', 'YandexImages', 'YandexVideo', 'YandexVideoParser',
            'YandexMedia', 'YandexBlogs', 'YandexFavicons', 'YandexWebmaster',
            'YandexPagechecker', 'YandexImageResizer','YandexAdNet', 'YandexDirect',
            'YaDirectFetcher', 'YandexCalendar', 'YandexSitelinks', 'YandexMetrika',
            'YandexNews', 'YandexNewslinks', 'YandexCatalog', 'YandexAntivirus',
            'YandexMarket', 'YandexVertis', 'YandexForDomain', 'YandexSpravBot',
            'YandexSearchShop', 'YandexMedianaBot', 'YandexOntoDB', 'YandexOntoDBAPI',
            'Googlebot', 'Googlebot-Image', 'Mediapartners-Google', 'AdsBot-Google',
            'Mail.RU_Bot', 'bingbot', 'Accoona', 'ia_archiver', 'Ask Jeeves',
            'OmniExplorer_Bot', 'W3C_Validator', 'WebAlta', 'YahooFeedSeeker', 'Yahoo!',
            'Ezooms', '', 'Tourlentabot', 'MJ12bot', 'AhrefsBot', 'SearchBot', 'SiteStatus',
            'Nigma.ru', 'Baiduspider', 'Statsbot', 'SISTRIX', 'AcoonBot', 'findlinks',
            'proximic', 'OpenindexSpider','statdom.ru', 'Exabot', 'Spider', 'SeznamBot',
            'oBot', 'C-T bot', 'Updownerbot', 'Snoopy', 'heritrix', 'Yeti',
            'DomainVader', 'DCPbot', 'PaperLiBot'
        );
        header('Location: https://www.site.ru/ru/');}
?>

感谢所有参与的人。获胜者安东德先生。非常感谢!

php
  • 3 3 个回答
  • 10 Views

3 个回答

  • Voted
  1. Best Answer
    Mr.AntonDer
    2022-06-06T04:51:35Z2022-06-06T04:51:35Z

    此代码确定请求是否来自机器人并将结果存储在$isBot变量中,然后执行您的代码:

    <?php
        
        require_once('config.php'); 
        
          $user_agent = $_SERVER["HTTP_USER_AGENT"];
          $options = [
            'YandexBot', 'YandexAccessibilityBot', 'YandexMobileBot','YandexDirectDyn',
            'YandexScreenshotBot', 'YandexImages', 'YandexVideo', 'YandexVideoParser',
            'YandexMedia', 'YandexBlogs', 'YandexFavicons', 'YandexWebmaster',
            'YandexPagechecker', 'YandexImageResizer','YandexAdNet', 'YandexDirect',
            'YaDirectFetcher', 'YandexCalendar', 'YandexSitelinks', 'YandexMetrika',
            'YandexNews', 'YandexNewslinks', 'YandexCatalog', 'YandexAntivirus',
            'YandexMarket', 'YandexVertis', 'YandexForDomain', 'YandexSpravBot',
            'YandexSearchShop', 'YandexMedianaBot', 'YandexOntoDB', 'YandexOntoDBAPI',
            'Googlebot', 'Googlebot-Image', 'Mediapartners-Google', 'AdsBot-Google',
            'Mail.RU_Bot', 'bingbot', 'Accoona', 'ia_archiver', 'Ask Jeeves',
            'OmniExplorer_Bot', 'W3C_Validator', 'WebAlta', 'YahooFeedSeeker', 'Yahoo!',
            'Ezooms', '', 'Tourlentabot', 'MJ12bot', 'AhrefsBot', 'SearchBot', 'SiteStatus',
            'Nigma.ru', 'Baiduspider', 'Statsbot', 'SISTRIX', 'AcoonBot', 'findlinks',
            'proximic', 'OpenindexSpider','statdom.ru', 'Exabot', 'Spider', 'SeznamBot',
            'oBot', 'C-T bot', 'Updownerbot', 'Snoopy', 'heritrix', 'Yeti',
            'DomainVader', 'DCPbot', 'PaperLiBot'
          ];
          foreach($options as $option)
          {
              if(!empty($option)){
            if(strpos($user_agent, $option) == true){
                $isBot = true;
                break;
            }else{
                $isBot = false;
            }
        }
            
          }
            
          if(!$isBot){
              
            $geoplugin = new geoPlugin();
            $geoplugin->locate();
       
            $var_country_code = $geoplugin->countryCode;
            if ($var_country_code == "RU") {
                header('Location: https://www.site.ru/ru/');
            }
            else if ($var_country_code == "US") {
                header('Location: https://www.site.ru/en/');
            }
            else if ($var_country_code == "UA") {
                header('Location: https://www.site.ru/ru/');
            }
            else if ($var_country_code == "KG") {
                header('Location: https://www.site.ru/ru/');
            }
            else if ($var_country_code == "UZ") {
                header('Location: https://www.site.ru/ru/');
            }
            else {
            header('Location: https://www.site.ru/en/');
            }
        }
        else{
            header('Location: https://www.site.ru/ru/');
        }
    ?>
    
    • 0
  2. Aleksey Vaganov
    2022-06-06T17:05:07Z2022-06-06T17:05:07Z

    你想要实现的东西叫做伪装,会受到搜索引擎的严厉惩罚。为了向搜索引擎发出关于网站语言版本可用性的信号,请使用帮助。

    • 0
  3. Daniel Protopopov
    2022-06-06T04:31:52Z2022-06-06T04:31:52Z

    这是工作代码

        <?php
    
    if (!empty($_SERVER['HTTP_USER_AGENT'])) {
        $options = array(
            'YandexBot', 'YandexAccessibilityBot', 'YandexMobileBot','YandexDirectDyn',
            'YandexScreenshotBot', 'YandexImages', 'YandexVideo', 'YandexVideoParser',
            'YandexMedia', 'YandexBlogs', 'YandexFavicons', 'YandexWebmaster',
            'YandexPagechecker', 'YandexImageResizer','YandexAdNet', 'YandexDirect',
            'YaDirectFetcher', 'YandexCalendar', 'YandexSitelinks', 'YandexMetrika',
            'YandexNews', 'YandexNewslinks', 'YandexCatalog', 'YandexAntivirus',
            'YandexMarket', 'YandexVertis', 'YandexForDomain', 'YandexSpravBot',
            'YandexSearchShop', 'YandexMedianaBot', 'YandexOntoDB', 'YandexOntoDBAPI',
            'Googlebot', 'Googlebot-Image', 'Mediapartners-Google', 'AdsBot-Google',
            'Mail.RU_Bot', 'bingbot', 'Accoona', 'ia_archiver', 'Ask Jeeves',
            'OmniExplorer_Bot', 'W3C_Validator', 'WebAlta', 'YahooFeedSeeker', 'Yahoo!',
            'Ezooms', '', 'Tourlentabot', 'MJ12bot', 'AhrefsBot', 'SearchBot', 'SiteStatus',
            'Nigma.ru', 'Baiduspider', 'Statsbot', 'SISTRIX', 'AcoonBot', 'findlinks',
            'proximic', 'OpenindexSpider','statdom.ru', 'Exabot', 'Spider', 'SeznamBot',
            'oBot', 'C-T bot', 'Updownerbot', 'Snoopy', 'heritrix', 'Yeti',
            'DomainVader', 'DCPbot', 'PaperLiBot'
        );
        if(in_array($_SERVER['HTTP_USER_AGENT'], $options)) {
            header('Location: https://www.yandex.ru/');
            return;
        }
    }
    
    header('Location: https://google.com');
    

    将最后一行替换为您的地理检查和重定向代码,并将https://www.yandex.ru替换为您需要的地址。

    • -2

相关问题

  • mysqli 类的对象无法转换为字符串

  • 您的系统中缺少 ext-http *,您的系统中缺少 ext-mysql_xdevapi *

  • 如何从csv中删除bom?

  • 当我按下 Enter 键时,如何让 PhpStorm 的 Emmet 插件触发,就像 VS Code 一样?

  • 注释在 Symfony5 中不起作用

  • 搜索最近的地理位置点

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5