How To Redirect Visitors By Country IP Address To Specific URL

Share:

How To Redirect Visitors By Country IP Address To Specific URL. For certain purposes, an example is to bring visitors who come from a particular country in a special page. May be to a page that supports the language of the visitor to be easy to understand or to the product page, service page or special offers for visitors from certain countries. For such purposes, we simply need a php script to redirect visitors to a country specific url based on their IP address.
How To Redirect Visitors By Country IP Address To Specific URL PHP ScriptHow do I do that ? Come on, we learn together how to redirect visitors by country IP Address to specific url. Read the tutorial below …

How To Redirect Visitors By Country IP Address To Specific URL

If you have studied the previous tutorial in PHP Script To Detect And Display Visitor’s Country Using His IP Address Location Lookup, in following this tutorial, you’ll have no trouble because the php script that we will use is the same. The difference is when the IP address can be identified visitors from a particular country then the visitor will automatically switch to a specific URL as practiced by major websites such as Google, Yahoo and even Blogger.
Here the php script to redirect visitors by country IP Address to spesific URL:
redirect-visitor-country-ip.php

<?php
$IPaddress=$_SERVER['REMOTE_ADDR'];
$two_letter_country_code=iptocountry($IPaddress);
include("ip_files/countries.php");
if ($two_letter_country_code == US){
header('location: http://'.$two_letter_country_code.'.example.com');
}else if($two_letter_country_code == ID){
header('location: http://'.$two_letter_country_code.'.example.com');
}else{
echo "HTML code from your homepage Website";
}
function iptocountry($ip) {
    $numbers = preg_split( "/\./", $ip);
    include("ip_files/".$numbers[0].".php");
    $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);
    foreach($ranges as $key => $value){
        if($key<=$code){
            if($ranges[$key][0]>=$code){$two_letter_country_code=$ranges[$key][1];break;}
            }
    }
    if ($two_letter_country_code==""){$two_letter_country_code="unkown";}
    return $two_letter_country_code;
}
?>

This php script will automatically divert visitors from the United State and Indonesia to a certain subdomain, but for those of you who use an IP address other than this country will remain in the website’s homepage.

If you want to redirect visitors to another URL specific, is easy enough. You just replace the existing URL in the header location with specific URL you want, an example :

header('location: http://example.com/'.$two_letter_country_code);        or
header('location: http://example.com/GiveAways-You-In-US);  

To add a redirect URL, for visitors from other countries, you only need to add the following php script :

}else if($two_letter_country_code == ID){
header('location: http://'.$two_letter_country_code.'.example.com');

put above of this script :

}else{
echo "HTML code from your homepage Website";
}

You replace the ID with the two letter country code of the destination country redirect, eg: IN for India, CA for Canada.
Don’t forget to upload ip_files folder in your website hosting, so that php script can be run on web browser very well. Download

Good luck 🙂
Thanks for reading How To Redirect Visitors By Country IP Address To Specific URL

[Tutorial.World.Edu]

7 thoughts on “How To Redirect Visitors By Country IP Address To Specific URL

Leave a Reply

Your email address will not be published. Required fields are marked *