最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Redirect to a subdirectory frontpage using without using a WP plugin- what files to edit, and how?

programmeradmin0浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论