Halloween Sale 15% off Ends in See the plans
PHP

Redirect Visitors by Country

Sends visitors from the countries you choose to a regional site or page, using the country header added by Cloudflare, CloudFront or your host, with search engine crawlers left alone.

Don’t have WPIDE yet? Install it on your site

Code

// Two-letter country code => address to send those visitors to. Empty = off. Examples:
// 'CA' => 'https://example.ca/',
// 'DE' => '/de/',
$wpide_country_redirects = array();
// true = only redirect visitors who land on the homepage; false = every page.
$wpide_country_homepage_only = true;
// Server headers that carry the visitor's country (Cloudflare, CloudFront, nginx GeoIP, many hosts).
$wpide_country_headers = array( 'HTTP_CF_IPCOUNTRY', 'HTTP_CLOUDFRONT_VIEWER_COUNTRY', 'HTTP_X_COUNTRY_CODE', 'GEOIP_COUNTRY_CODE' );

add_action( 'template_redirect', function () use ( $wpide_country_redirects, $wpide_country_homepage_only, $wpide_country_headers ) {
    if ( ! $wpide_country_redirects || is_user_logged_in() || is_feed() || is_robots() || ( $wpide_country_homepage_only && ! is_front_page() ) ) {
        return;
    }

    // ?stay=1 remembers that the visitor wants this version of the site for 30 days.
    if ( isset( $_GET['stay'] ) ) {
        setcookie( 'wpide_geo_stay', '1', time() + 30 * DAY_IN_SECONDS, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, is_ssl(), true );
        return;
    }

    if ( isset( $_COOKIE['wpide_geo_stay'] ) ) {
        return;
    }

    $agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) ) : '';

    // Never redirect search engine crawlers, so every version stays indexable.
    if ( '' === $agent || preg_match( '/bot|crawl|spider|slurp|facebookexternalhit|preview/', $agent ) ) {
        return;
    }

    $country = '';

    foreach ( $wpide_country_headers as $header ) {
        if ( ! empty( $_SERVER[ $header ] ) ) {
            $country = strtoupper( substr( sanitize_text_field( wp_unslash( $_SERVER[ $header ] ) ), 0, 2 ) );
            break;
        }
    }

    if ( '' === $country || empty( $wpide_country_redirects[ $country ] ) ) {
        return;
    }

    $target = $wpide_country_redirects[ $country ];
    $target = preg_match( '#^https?://#i', $target ) ? $target : home_url( $target );

    nocache_headers();
    wp_redirect( esc_url_raw( $target ), 302 ); // phpcs:ignore WordPress.Security.SafeRedirect -- targets are set by the site owner.
    exit;
}, 1 );

Notes

Off until you add countries. Needs a host or CDN that sends a country header; page caches must vary by country. Visitors can opt out with ?stay=1.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.