2012-03-23 127 views
18

我做了一個網站,它與一些子域名;根據該國的IP地址,用戶應該被自動重定向到相應的子域名。如何根據國家IP地址重定向域名

例子:

主要網站是abcd.com

  • 假設從印度有人輸入這個網址abcd.com,
  • ,則頁面重定向到ind.abcd.com
+0

GeoIP擴展允許您查找IP地址的位置。 http://www.php.net/manual/en/book.geoip.php – 2012-03-23 11:31:20

回答

20

檢查您是否有mod_geoip模塊(GeoIP分機)安裝在您的服務器上。

然後,相應地調整你的.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... 

而這裏的official documentation

+0

如何通過cpanel安裝它? – Firefog 2016-08-02 10:34:39

19

從下載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/'); 
} 
?> 

這裏是國家代碼列表:

http://www.geoplugin.com/iso3166

9

你可以做到這一點沒有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') ; 

?> 
0

如果您在使用WordPress網站,所以其易於使用 - (GEO重定向插件)。它像魅力一樣工作。易於使用,易於實施。