2012-03-23 6 views
18

Tôi đã tạo trang web với một số tên miền phụ; theo địa chỉ IP của quốc gia mà người dùng được cho là được tự động chuyển hướng đến tên miền phụ tương ứng.cách chuyển hướng miền theo địa chỉ IP quốc gia

Ví dụ:

trang web chính là abcd.com

  • Giả sử một số một từ Ấn Độ gõ url này abcd.com,
  • sau đó trang chuyển hướng đến ind.abcd.com
+0

Tiện ích GeoIP cho phép bạn tìm vị trí của địa chỉ IP. http://www.php.net/manual/en/book.geoip.php –

Trả lời

20

Kiểm tra xem bạn cókhôngmô-đun (Mở rộng GeoIP) được cài đặt trên máy chủ của bạn.

Sau đó, tinh chỉnh tập tin .htaccess bạn cho phù hợp:

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... 

Và đây là official documentation.

+0

Làm thế nào để cài đặt nó qua cPanel? – Firefog

19

Tải lớp geoPlugin từ:

http://www.geoplugin.com/_media/webservices/geoplugin.class.phps

Đặt một tập tin index.php trong thư mục gốc của web site:

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

Dưới đây là danh sách các mã quốc gia:

http://www.geoplugin.com/iso3166

9

Bạn có thể làm điều này mà không require_once('geoplugin.class.php'); như vậy:

<?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

Nếu bạn đang sử dụng một trang web WordPress, vì vậy nó dễ dàng để sử dụng - (Geo Redirect plugin). Nó hoạt động như sự quyến rũ. Dễ sử dụng, dễ thực hiện.