网站有 3 页
- 网站.ru
- site.ru/ru/
- 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/');}
?>
感谢所有参与的人。获胜者安东德先生。非常感谢!
此代码确定请求是否来自机器人并将结果存储在$isBot变量中,然后执行您的代码:
你想要实现的东西叫做伪装,会受到搜索引擎的严厉惩罚。为了向搜索引擎发出关于网站语言版本可用性的信号,请使用帮助。
这是工作代码
将最后一行替换为您的地理检查和重定向代码,并将https://www.yandex.ru替换为您需要的地址。