【问题标题】:Country based redirect only working for staging site not live site基于国家/地区的重定向仅适用于临时站点而不是实时站点
【发布时间】:2015-09-04 16:01:28
【问题描述】:

我正在尝试: 根据访问者的 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 登台服务器上,这个解决方案完美无缺,您可以自己测试它http://centuri.staging.wpengine.com,但是当这个脚本应用于我的实时服务器时,我被重定向到http://www.centuri.se/en/en,并会收到一条 404 消息 - 我已经尝试过将重定向的一部分从 home_url() 切换到 site_url() 以查看任何区别,但如果我在我的实时服务器上这样做,这会给我一个重定向循环。我现在已经为我的实时网站评论了这一点,因为它会使我的网站崩溃。

这可以是 WPML 中的任何设置吗?我真的不知道从哪里开始......这太令人困惑了,因为它在我的登台服务器上运行完美,而不是在我的实时服务器上运行,并且代码和数据库是相同的。

【问题讨论】:

    标签: php .htaccess redirect country wpml


    【解决方案1】:

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

    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 操作中运行(如果您直接在函数中运行它。 php 文件,$country = $geo-&gt;country(); 将返回 NULL)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-06
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多