Geo-Redirect Visitors to a Localized Website

Sheharyar Malik
By Sheharyar Malik Senior Software Engineer
Posted on March 6, 2024 | 3 min read
Geo-Redirect Visitors to a Localized Website
Back to All Blogs

Is your business present in more than one regions in the world? Do you have a customer base in multiple regions of the world? If yes, then you must have the website visitors from all over the world.

When the visitors hit your business website, do they see:

  • a dedicated page for their location (Geo-marketing)
  • the content on your website in their local language (Content Localization)
  • the products' prices in their local currency (Local Pricing)

You're missing tremendous opportunities on geotargeting, if you're doing none of these things. Your visitors face a barrier of language, currency or products unrelated to their region. That enhances the friction between your business and the buyers.

IP location services make location-based marketing easier for online sellers. Here are the steps to geo-target a visitor on your business site:

  1. Get the visitor's IP-location
  2. Validate the visitor's region and see if you're geotargeting the visitors from that region
  3. Redirect or display the localized content to the visitor

Here is how to do it using ipgeolocation.io API:

Before moving on to code examples, you need an API key to use ipgeolocation.io API. If you do not have an account at ipgeolocation.io, then:


Geotarget Your Visitors using JQuery SDK

Open a text editor and open the page, where you want to display localized content, of your website, say index.html.

Add the following script in the head tag of the page.

<script src="https://cdn.jsdelivr.net/npm/ip-geolocation-api-jquery-sdk@1.1.2/ipgeolocation.min.js"></script>

Add another script block in the head tag of the page.

<script>
// Enable sessionStorage usage to store API response on client-side. This avoids duplicate API calls for a visitor visiting multiple pages during single visit disabling.
_ipgeolocation.enableSessionStorage(true);

// Disable async calls to ipgeolocation.io API.
_ipgeolocation.makeAsyncCallsToAPI(false);

// Fetch only the `country_code2` field from the response, excluding rest of the response
_ipgeolocation.setFields("country_code2");

// Get ipgeolocation for the visitor's IP address. Replace "YOUR_API_KEY" with the API key from the ipgeolocation.io dashboard.
_ipgeolocation.getGeolocation(redirectToLocalizedWebsites, "YOUR_API_KEY");

function redirectToLocalizedWebsites(ipGeolocationResponse) {
  country_code2 = ipGeolocationResponse.country_code2;

  // if the visitor is from US and he isn't visiting the US website, then redirect to us.site.com US website
  if (country_code2 === 'US' && window.location.hostname !== "us.site.com") {
    window.location.href = "https://us.site.com/";
  } else if (country_code2 === 'CA' && window.location.hostname !== "ca.site.com") {
    window.location.href = "https://ca.site.com/";
  } else if (country_code2 === 'AU' && window.location.hostname !== "au.site.com") {
    window.location.href = "https://au.site.com/";
  } else if (window.location.hostname !== "global.site.com") {
    window.location.href = "https://global.site.com/";
  }
}
</script>

Geo-redirecting Your Visitors using PHP

Open the home page of your website, say index.php, in a text editor.

Add the following script in the head tag of the page.

<?php
// query ipgeolocation.io API and returns JSON response
function get_geolocation($apiKey, $ip, $lang = "en", $fields = "*") {
  $url = "https://api.ipgeolocation.io/v3/ipgeo?apiKey=".$apiKey."&ip=".$ip."&lang=".$lang."&fields=".$fields;  
  $cURL = curl_init();
  
  curl_setopt($cURL, CURLOPT_URL, $url);
  curl_setopt($cURL, CURLOPT_HTTPGET, true);
  curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($cURL, CURLOPT_HTTPHEADER, array (
    "Accept: application/json"
  ));

  return curl_exec($cURL);
}

// get ipgeolocation for the visitor's IP address. Replace YOUR_API_KEY with your API key.
$json = get_geolocation("YOUR_API_KEY", $_SERVER["REMOTE_ADDR"], "en", "country_code2");
$geolocation = json_decode($json, true);
$currentWebsite = $_SERVER["SERVER_NAME"];
$redirect = false;
$redirectTo = null;

if ($geolocation["country_code2"] == "US" && $currentWebsite != "us.site.com") {
  $redirect = true;
  $redirectTo = "https://us.site.com/";
} else if ($geolocation["country_code2"] == "CA" && $currentWebsite != "ca.site.com") {
  $redirect = true;
  $redirectTo = "https://ca.site.com/";
} else if ($geolocation["country_code2"] == "AU" && $currentWebsite != "au.site.com") {
  $redirect = true;
  $redirectTo = "https://au.site.com/";
} else if ($currentWebsite != "global.site.com") {
  $redirectTo = "https://global.site.com/";
}

if ($redirect) {
  header("Location: ".$redirectTo);
  die();
}
?>

Related Articles

iCloud Private Relay vs Cloudflare WARP, Explained
iCloud Private Relay vs Cloudflare WARP, Explained

iCloud Private Relay and Cloudflare WARP both hide a visitor's real IP address, but they are built for different jobs and hide it in different ways. Private Relay is a Safari-only, two-hop privacy relay. WARP is a whole-device tunnel through Cloudflare's network. Both keep the user near their real region, and, as you will see below, our detection API flags both as relays. If you run a website, an app, or a fraud pipeline, you meet both of these in your logs, not in an app store. This piece look

Posted onJuly23, 2026
Read More
IP Geolocation Data: Built, Not Guessed
IP Geolocation Data: Built, Not Guessed

A lot of weak "IP geolocation data" is just a whois guess dressed up with marketing. Here is how we actually build ours, layer by layer, from registry data and geofeeds to active measurement and verification, and how accurate it really is.

Posted onJuly13, 2026
ByMudassar Tariq
Read More
What Is a CGI Proxy and Is It Still a Threat?
What Is a CGI Proxy and Is It Still a Threat?

A CGI proxy is an old-school web proxy you use through a web page, and it leaves an obvious trace. The destination sees the proxy server's IP, not the visitor's. Here is how it works, whether it still matters, and how to flag the traffic.

Posted onJune10, 2026
Read More
Benefits of IP Geolocation: 8 Real Business Outcomes
Benefits of IP Geolocation: 8 Real Business Outcomes

Eight benefits of IP geolocation that translate directly into revenue, security, and compliance outcomes, with an industry matrix showing which ones matter most for eCommerce, SaaS, AdTech, Fintech, Streaming, and regulated industries.

Posted onJune9, 2026
Read More
Your VPN Isn't Secret: How We Detect Risky & Hidden IPs
Your VPN Isn't Secret: How We Detect Risky & Hidden IPs

VPN detection is only as good as its data. We monitor 160+ providers, run live honeypots, and update threat scores daily. Most APIs won't tell you this much.

Posted onMay21, 2026
Read More
Top 10 IP Geolocation Use Cases in 2025 for Businesses and Developers
Top 10 IP Geolocation Use Cases in 2025 for Businesses and Developers

Discover the most impactful IP geolocation use cases in 2025 – from personalized content and fraud detection to compliance and threat intelligence.

Posted onApril29, 2026
Read More

Subscribe to Our Newsletter

Get the latest in geolocation tech, straight to your inbox.