13033 – How to redirect users to a different URL based on their location

This article will cover the method using which you can redirect users to a different URL based on their location.

If you want to redirect your site visitors to different pages based on their country location simply modify the code below and insert it on the header of your page

You need some per-requisites before putting it all in place. You need to have created the pages you want to add to the script beforehand, since you will need valid URLs for the pages.
 

How it works:

When someone lands on the page the code will check which country they are located and will redirect them to a specific url.

Header code template:

<script type=”text/javascript” src=”https://YOUR_DOMAIN_NAME/geo.php”></script>

<script type=”text/javascript”>

var parameteresLine = window.location.search;

if (geoplugin_countryCode() == ‘XX’) {

document.location.href = ‘URL_HERE’ + parameteresLine;

} else if (geoplugin_countryCode() == ‘XX’) {

document.location.href = ‘URL_HERE’ + parameteresLine;

} else {

document.location.href = ‘DEFAULT_URL_HERE’ + parameteresLine;

}

</script>

 


You will need to edit it as follows

+ Insert your own domain name instead of “https://YOUR_DOMAIN_NAME

+ Replace XX with the country code you are targeting. Here is the list of country codes: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements

+ Insert the page URL for each country code instead of URL_HERE .

+ Insert a default page URL users will reach if they don’t match any of the other criteria instead of “DEFAULT_URL_HERE

How to add more rule:

If you need to add more rules simply add

} else if (geoplugin_countryCode() == ‘XX’) {

document.location.href = ‘URL_HERE’ + parameteresLine;

}

before the last } else {

 


How to add the code on your page:

Once you have edited the above script then you are ready to add it to the main page.

After you have created your page and you are happy, on one of your pages, you must go to the “Page Settings” on the left hand side, expand the section called “Header, Footer and After Body Tag Code” and paste the following code in the “Header Code” section: