基于国家的重定向只适用于分期站点而不是现场


Country based redirect only working for staging site not live site

我想:根据访问者的IP进行重定向,我有我的网站托管在wpengine,他们有一个GEO IP服务,我可以根据他们的IP获得用户的国家。如果您从瑞典、丹麦或挪威访问该网站,您将不会被重定向,而是停留在页面上。http://www.centuri.se但如果你不是来自这个国家,你将被重定向到英文版本的网站,翻译为wpml…所以你会去这个页面…http://www.centuri.se/en/是翻译后的那个

我正在使用这段代码来进行重定向。

<?php
// THE COOKIE NAME
$cookie_name = "country";
// ACCEPTED COUNTRIES THAT SKIPS THE REDIRECT
$countries = array('se','dk','no');
// CHECK IF YOUR COOKIE IS SET
if (!isset($_COOKIE[$cookie_name])) {
  // GET USER INFO
$userInfo = do_shortcode('[geoip-country]');
// GET COUNTRY INTO LOWERCASE
$country = strtolower($userInfo);
//SET COOKIE BASED ON COUNTRY NAME FROM USER
setcookie('country', $country, time() + (3600 * 24 * 30), '/');
  if(!in_array($country, $countries)) {
      //Set a cookie to tell that this user has been redirected
      setcookie('redirect', 1, time() + (3600 * 24 * 30), '/');
      wp_redirect( home_url() . '/en/' ); exit;
  }
  }
  ?>

我交付准备服务器从wpengine sollution完美的工作,您可以测试自己http://centuri.staging.wpengine.com服务器但当这个脚本适用于我的生活我重定向到http://www.centuri.se/en/en并将于404年得到一个消息,我试图把重定向的一部分从home_url site_url()()而不是看到不会有什么不同,但如果我做住服务器,这将给我一个重定向循环。我现在已经把这个注释掉了,因为它会使我的网站崩溃。

可以在WPML中进行任何设置吗?我真的不知道接下来该怎么办……这是如此令人困惑,因为它的工作完美无瑕在我的暂存服务器,而不是我的实时服务器和代码和数据库是相同的。

我还没有测试过这些,但是,这里有几个想法:

1)您可能只需要添加当前WPML语言的检查,以确保一旦用户已经在EN站点上,您不会继续重定向用户。

2)您设置了$_COOKIE['redirect'],但没有对其进行任何操作。你可以检查你正在检查其他cookie的地方(我不确定cookie会被设置并立即可用,尽管如果你在循环中重定向)。

<?php
    // THE COOKIE NAME
    $cookie_name = "country";
    // ACCEPTED COUNTRIES THAT SKIPS THE REDIRECT
    $countries = array('se','dk','no');
    // CHECK IF YOUR COOKIE IS SET
    if (
        !isset( $_COOKIE[$cookie_name] )
        // Check redirect cookie??
        // && !isset( $_COOKIE['redirect'] )
    ) {
        // GET USER INFO
        $userInfo = do_shortcode('[geoip-country]');
        // GET COUNTRY INTO LOWERCASE
        $country = strtolower($userInfo);
        // SET COOKIE BASED ON COUNTRY NAME FROM USER
        setcookie('country', $country, time() + (3600 * 24 * 30), '/');
        if (
            // Don't redirect if current country is in skip list
            !in_array($country, $countries)
            // Also Check WPML lang code global to make sure the user
            // isn't already on the english site
            && ICL_LANGUAGE_CODE !== 'en'
        ) {
          // Set a cookie to tell that this user has been redirected
          setcookie( 'redirect', 1, time() + (3600 * 24 * 30), '/' );
          wp_redirect( home_url() . '/en/' ); exit;
        }
    }

我最近刚刚实现了WPEngine GeoIP检查自己,你可能会考虑的另一件事是直接使用他们的PHP类而不是do_shortcode(似乎这可能对你有用,但这里有另一种方法)。

add_action( 'init', 'country_geo_redirect' );
function country_geo_redirect() {
    $geo = WPEngine'GeoIp::instance();
    $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    if (
        !is_admin() && // Let's not redirect the admin panel
        !in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) &&
        $_SERVER['HTTP_HOST'] !== 'example.com.au' && // Make sure we aren't already on this domain
        (
            strpos( strtolower( $lang ), 'en-au' ) > -1 // Check the user's browser languages for what we are targeting
            || $geo->country() == 'AU' // Fallback to using WPEngine's GeoIP Check if Browser lang doesn't include en-AU
        )
    ) {
        wp_redirect( 'http://example.com' . $_SERVER['REQUEST_URI'] , 301 );
        exit;
    }
}

你必须记住,虽然使用他们的类($geo = WPEngine'GeoIp::instance();)它不能确定用户的国家,除非它在init行动中运行(如果你直接在你的functions.php文件中运行,$country = $geo->country();将返回NULL)。

对于第二个示例,我还首先检查用户的浏览器语言,看看它是否包含目标语言(在本例中为en-AU),以便我可以跳过GeoIP查找(如果查找有任何延迟)以提高加载速度(尚未实际测试查找时间)。