# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Redirects](https://wpide.com/library/category/redirects/)
- Tags: redirects, geolocation, international
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/country-redirects/

## Code

```php
// 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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Redirect Visitors by Country" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
