php 如何根据国家IP地址重定向域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9838344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to redirect domain according to country IP address
提问by user1288065
I made a site it with some subdomains; according to the country's IP address the user is supposed to be automatically redirected to corresponding subdomain.
我用一些子域创建了一个站点;根据国家/地区的 IP 地址,用户应该被自动重定向到相应的子域。
Example :
例子 :
Main site is abcd.com
主要站点是 abcd.com
- Suppose some one from Indiatyped this url abcd.com,
- then the page redirects to
ind.abcd.com
- 假设来自印度的某个人 输入了这个网址 abcd.com,
- 然后页面重定向到
ind.abcd.com
回答by Porta Shqipe
Download the geoPlugin class from:
从以下位置下载 geoPlugin 类:
http://www.geoplugin.com/_media/webservices/geoplugin.class.phps
http://www.geoplugin.com/_media/webservices/geoplugin.class.phps
Put a index.phpfile in your root folder:
将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/');
}
?>
Here is a list of country codes:
以下是国家代码列表:
回答by Dr.Kameleon
Check that you have the mod_geoipmodule (GeoIP Extension) installed on your server.
检查您的服务器上是否安装了mod_geoip模块(GeoIP Extension)。
Then, tweak your .htaccess
file accordingly :
然后,相应地调整您的.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 [L]
# India
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^IN$
RewriteRule ^(.*)$ http://in.abcd.com [L]
# etc etc etc...
And here's the official documentation.
这是官方文档。
回答by Ehsan88
You could do this without require_once('geoplugin.class.php');
like so:
你可以不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' ) ;
?>
回答by Grapy
If you are using a WordPress website, so its easy to use - (Geo Redirect plugin). It's working like charm. Easy to use, easy to implement.
如果您使用的是 WordPress 网站,那么它很容易使用 - (地理重定向插件)。它像魅力一样工作。易于使用,易于实施。