How to Redirect Website by User Location in WordPress?

If you’re running a WordPress website and want to automatically redirect users based on their country or region — for example, from example.com to example.co.uk for UK visitors — you’re in the right place.

In this post, we’ll walk through several PHP-based methods to set up geo-redirects in WordPress. Whether you’re a developer or a DIY website owner, we’ll show you practical ways to detect user location and redirect them to the correct domain — without hurting your SEO.


Why Geo-Redirects Matter for WordPress Sites

Redirecting visitors to the right version of your site based on location can seriously improve:

  • 🔥 User experience – Serve the right language, currency, or content.
  • 📈 Conversion rates – People are more likely to buy when the site feels local.
  • 🧠 SEO – When done right, it helps Google understand which site version is for which audience.

Let’s dive into the methods.


Method 1: PHP Geo-Redirect Using MaxMind GeoIP2

This is a powerful, server-side method that uses an IP database to detect the user’s country.

✅ How it works:

You download a GeoIP database and use PHP in your WordPress theme or custom plugin to handle the redirects.

🧩 Setup Steps:

  1. Download GeoLite2 Database
    Create a MaxMind account and download the free GeoLite2 Country database.
  2. Install the GeoIP2 PHP Library
    If you’re using Composer: composer require geoip2/geoip2
  3. Add Redirect Logic to functions.php:
function geoip_country_redirect() {
    if (is_admin() || php_sapi_name() === 'cli') return;

    require_once __DIR__ . '/vendor/autoload.php';

    use GeoIp2\Database\Reader;

    $reader = new Reader(__DIR__ . '/GeoLite2-Country.mmdb');

    $ip = $_SERVER['REMOTE_ADDR'];

    try {
        $record = $reader->country($ip);
        $countryCode = $record->country->isoCode;

        if ($countryCode === 'GB' && strpos($_SERVER['HTTP_HOST'], 'co.uk') === false) {
            wp_redirect('https://example.co.uk' . $_SERVER['REQUEST_URI'], 302);
            exit;
        }

        if ($countryCode === 'CA' && strpos($_SERVER['HTTP_HOST'], 'ca') === false) {
            wp_redirect('https://example.ca' . $_SERVER['REQUEST_URI'], 302);
            exit;
        }

    } catch (Exception $e) {
        // Optional: log errors or ignore silently
    }
}
add_action('init', 'geoip_country_redirect');

Method 2: Use a WordPress Geo Targeting Plugin

If you don’t want to mess with code, plugins can make geo-redirects a breeze.

🔌 Popular options:

These plugins usually let you:

  • Create rules based on country
  • Redirect users to different domains or pages
  • Whitelist bots like Google to avoid SEO penalties

✅ Great for non-developers
❌ May add load time depending on the plugin


Method 3: Use PHP + an IP Location API (No DB Needed)

This is a lighter method — instead of downloading a database, use an external API to fetch user location.

Example using ipapi.co:

function api_geo_redirect() {
    if (is_admin()) return;

    $ip = $_SERVER['REMOTE_ADDR'];
    $location = json_decode(file_get_contents("https://ipapi.co/{$ip}/json/"));

    if (!empty($location->country)) {
        if ($location->country === 'AU' && strpos($_SERVER['HTTP_HOST'], 'com.au') === false) {
            wp_redirect('https://example.com.au' . $_SERVER['REQUEST_URI'], 302);
            exit;
        }
    }
}
add_action('init', 'api_geo_redirect');

✅ No need to manage a database
❌ Slower — depends on third-party response times
❌ May fail without fallback logic


Method 4: Redirect Based on Language Headers

Sometimes, using browser language settings is enough to suggest a redirect.

function language_based_redirect() {
    if (is_admin()) return;

    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

    if ($lang === 'fr' && strpos($_SERVER['HTTP_HOST'], 'fr') === false) {
        wp_redirect('https://fr.example.com' . $_SERVER['REQUEST_URI'], 302);
        exit;
    }
}
add_action('init', 'language_based_redirect');

✅ No IP detection needed
❌ Less accurate than IP-based
❌ Users may have browser languages set incorrectly


SEO Best Practices for Geo-Redirects

Whatever method you choose, don’t forget your SEO hygiene:

  • Use 302 (temporary) redirects, not 301 — you don’t want to confuse search engines.
  • Don’t redirect Googlebot or other crawlers. Let them crawl all versions.
  • Use hreflang tags to tell search engines which version is for which audience.
  • Give users a manual option to switch regions/languages.

Example hreflang tags:

<link rel="alternate" href="https://example.com" hreflang="en" />
<link rel="alternate" href="https://example.co.uk" hreflang="en-gb" />
<link rel="alternate" href="https://example.ca" hreflang="en-ca" />

Final Thoughts

Redirecting users by country in WordPress is easier than it sounds. Whether you go with a full GeoIP database, a lightweight API, or a plugin solution, the key is doing it cleanly — and keeping both users and search engines happy.

TL;DR:

  • Use PHP with GeoIP2 for full control
  • Go plugin route for simplicity
  • Consider APIs for lightweight sites
  • Don’t forget SEO essentials

Need help setting up redirects on your WordPress site? Drop a comment or reach out — I’d be happy to guide you!