use*_*065 31 php .htaccess redirect geotargetting geolocation
我用一些子域名建立了一个网站; 根据国家/地区的IP地址,用户应自动重定向到相应的子域.
示例:
主要网站是 abcd.com
ind.abcd.com
Por*_*ipe 28
从以下位置下载geoPlugin类:
http://www.geoplugin.com/_media/webservices/geoplugin.class.phps
将index.php文件放在根文件夹中:
<?php
require_once('geoplugin.class.php');
$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 == "AL") {
header('Location: http://sq.wikipedia.org/');
}
else if ($var_country_code == "NL") {
header('Location: http://nl.wikipedia.org/');
}
else {
header('Location: http://en.wikipedia.org/');
}
?>
Run Code Online (Sandbox Code Playgroud)
以下是国家/地区代码列表:
http://www.geoplugin.com/iso3166
Dr.*_*eon 26
检查您的服务器上是否安装了mod_geoip模块(GeoIP Extension).
然后,相应地调整您的.htaccess
文件:
GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat
# Start Redirecting countries
# Canada
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
RewriteRule ^(.*)$ http://ca.abcd.com$1 [L]
# India
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^IN$
RewriteRule ^(.*)$ http://in.abcd.com$1 [L]
# etc etc etc...
Run Code Online (Sandbox Code Playgroud)
这是官方文档.
Ehs*_*n88 17
你可以这样做而不是require_once('geoplugin.class.php');
这样:
<?php
$a = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$countrycode= $a['geoplugin_countryCode'];
if ($countrycode=='US')
header( 'Location: http://example1.com' ) ;
else
header( 'Location: http://example2.com' ) ;
?>
Run Code Online (Sandbox Code Playgroud)