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 : English language frontpage
mywebsite/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 , 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('.gp?ip='.$_SERVER['REMOTE_ADDR']));
$countrycode= $a['geoplugin_countryCode'];
if ($countrycode=='JP'){
header( 'Location: /' );
exit;
}
The problem
However, I get the following error
The page isn’t redirecting properly
An error occurred during a connection to mywebsite.
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('.gp?ip='.$_SERVER['REMOTE_ADDR']));
$countrycode= $a['geoplugin_countryCode'];
if ($countrycode=='JP'){
header( 'Location: ' );
exit;
}
Is there a way to fix this error?
I am also open to other solutions not using a WP plugin.
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 : English language frontpage
mywebsite/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 , 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/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$countrycode= $a['geoplugin_countryCode'];
if ($countrycode=='JP'){
header( 'Location: https://mywebsite/jp/' );
exit;
}
The problem
However, I get the following error
The page isn’t redirecting properly
An error occurred during a connection to mywebsite.
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/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$countrycode= $a['geoplugin_countryCode'];
if ($countrycode=='JP'){
header( 'Location: https://anotherwebsite' );
exit;
}
Is there a way to fix this error?
I am also open to other solutions not using a WP plugin.
Share Improve this question edited Feb 17, 2020 at 14:37 yarimoto asked Feb 17, 2020 at 14:27 yarimotoyarimoto 1033 bronze badges 2- Care to explain the downvote? – yarimoto Commented Feb 17, 2020 at 15:12
- Perhaps your host doesn't allow 'file_get_contents'. Test the value of $countrycode to see what is being returned. – Rick Hellewell Commented Mar 13, 2020 at 22:45
1 Answer
Reset to default 0It sounds like every time any page is loaded, if it meets the condition, it will try to redirect. So, if you're viewing from Japan, and you hit the top-level site, you'll be redirected. But then when you are redirected, the header.php
code once again will see that you're viewing from Japan, and try to redirect. And so on, and so forth.
So a solution to the immediate problem would be to add a condition. Only try to redirect if the visitor is currently viewing the top-level site.
<?php
// Only if this is the top-level site
if(get_current_blog_id() == 1) {
$a = unserialize(file_get_contents('http://www.geoplugin/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$countrycode= $a['geoplugin_countryCode'];
if ($countrycode=='JP'){
header( 'Location: https://mywebsite/jp/' );
exit;
}
}
?>
However, this may not be ideal. If you have content that other visitors will be viewing on the top-level site, every time they load a page, this check is going to run and slow things down. So, it would be much better to find a way to cookie visitors and not be running server-side logic every time a page is loaded.
Also, if you've added this code to an existing theme's header.php
file, it's going to be overwritten every time you update the theme. So you'll want to create a child theme if this is still the approach you want to take.