How to redirect visitor to country specific website

How to redirect visitor to your country specific website

  • June 1, 2022

Today we will talk about how to redirect visitors to your country specific website using custom snippet.

This snippet will work on your WordPress website.

/**
 * @snippet       Redirect visitor to country specific website
 * @author        Deep Prakash Goyal
 * @website       https://wpexpertdeep.com/
 */

add_action('wp_head', 'redirect_according_visiter_country');

function redirect_according_visiter_country(){
    $a = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
    $countrycode= $a['geoplugin_countryCode'];
    if ($countrycode=='IN'){
        header( 'Location: /in' . $_SERVER['REQUEST_URI'] );
    }else{
        header( 'Location: https://example.com');
    }
}

If your website is a non wordpress website then use this part of the code

$a = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
    $countrycode= $a['geoplugin_countryCode'];
    if ($countrycode=='IN'){
        header( 'Location: /in' . $_SERVER['REQUEST_URI'] );
    }else{
        header( 'Location: https://example.com');
    }

If this snippet not work on your server, use ob_start() function starting of the snippet and end with ob_end_clean();

Related Post

How to Create a Blog Website on WordPress

How to Create a Blog Website on WordPress

If you’re looking to start a blog website, WordPress is an excellent platform to use. It’s easy to use, customizable, and has a large community of users who can help you out if you run […]

Fix-Images-and-Broken-Links-by-Updating-Paths-in-WordPress

Fix Images and Broken Links by Updating Paths in WordPress

Today we will learn how to fix broken images link issue in WordPress, after migrating site from local server to live server or (one domain to another). This is a very common issue. The simple […]

Insert post programmatically in WordPress

Insert post programmatically in WordPress

Today we are gonna learn how to insert post in database programmatically. The function we are going to use is wp_insert_post() For creating a post first you need to create a post array, below is […]