Created
January 4, 2018 12:46
-
-
Save OkoyaUsman/f27c39442befc900778c964af9881e1a to your computer and use it in GitHub Desktop.
Redirect domain according to country IP address using php or .htaccess
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Suppose you have three sub domain like http://au.example.com, http://ng.example.com, http://in.example.com Australia, Nigeria, India. | |
And If some one type http://example.com then he/she redirected county specific url as per geo ip location. Here is the solution. | |
// By PHP | |
First of all download geoplugin library and call it on your page. | |
http://www.geoplugin.com/_media/webservices/geoplugin.class.phps | |
<?php | |
require_once('geoplugin.class.php'); | |
$geoplugin = new geoPlugin(); | |
$geoplugin->locate(); | |
$countryCode = $geoplugin->countryCode; | |
switch($countryCode) { | |
case AU: | |
header('Location: http://au.example.com/'); | |
break; | |
case NG: | |
header('Location: http://ng.example.com/'); | |
break; | |
case IN: | |
header('Location: http://in.example.com/'); | |
break; | |
} | |
?> | |
// Here is the country code list for geoplugin and you can find easily. | |
http://www.geoplugin.com/iso3166 | |
// By .htaccess | |
First you need to check that you have the mod_geoip module (GeoIP Extension) installed and configured on your server. | |
If yes then tweak your .htaccess file accordingly. | |
# start code | |
GeoIPEnable On | |
GeoIPDBFile /path/to/GeoIP.dat | |
# Start Redirecting countries | |
# Australia | |
RewriteEngine on | |
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AU$ | |
RewriteRule ^(.*)$ http://au.example.com$1 [L] | |
# Nigeria | |
RewriteEngine on | |
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^NG$ | |
RewriteRule ^(.*)$ http://ng.example.com$1 [L] | |
# India | |
RewriteEngine on | |
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^IN$ | |
RewriteRule ^(.*)$ http://in.example.com$1 [L] | |
# and so on. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment