Redirect to a subdirectory frontpage using without using a WP plugin- what files to edit, and how?
My situation
I have a multisite setup, where I use subdirectories for different languages. I use the main homepage for English and subdirectories for other languages, as follows:
mywesbite.com : English language frontpage
mywebsite.com/jp/ : Japanese language frontpage
What I want to do
I would like to redirect people who live in Japan to the Japanese language ffrontpage without using a WordPress plugin.
What I have tried
Using the geoplugin.com , as inspired by the 3rd answer of this stack I edited the theme header.php
file by adding the following at the very beginning
$a = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$countrycode= $a['geoplugin_countryCode'];
if ($countrycode=='JP'){
header( 'Location: https://mywebsite.com/jp/' );
exit;
}
The problem
However, I get the following error
The page isn’t redirecting properly
An error occurred during a connection to mywebsite.com.
This problem can sometimes be caused by disabling or refusing to accept cookies.
Note: the redirecting works fine with an external website, for example
$a = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$countrycode= $a['geoplugin_countryCode'];
if ($countrycode=='JP'){
header( 'Location: https://anotherwebsite.com' );
exit;
}
Is there a way to fix this error?
I am also open to other solutions not using a WP plugin.
Leave an answer