
How to redirect visitor to your country specific website
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();