Php 为什么带重定向的add_过滤器会导致无限重定向循环?

Php 为什么带重定向的add_过滤器会导致无限重定向循环?,php,wordpress,Php,Wordpress,我可以看到下面的代码是如何有问题的,但不知道为什么它会导致重定向循环 //基于位置\u cookie重定向用户 add_filter( 'after_setup_theme', 'ip_redirect_on_cloudflare', 1); function ip_redirect_on_cloudflare() { $CFCountry = $_SERVER['HTTP_CF_IPCOUNTRY']; switch ($CFCountry){ case "S

我可以看到下面的代码是如何有问题的,但不知道为什么它会导致重定向循环

//基于位置\u cookie重定向用户

add_filter( 'after_setup_theme', 'ip_redirect_on_cloudflare', 1);
function ip_redirect_on_cloudflare()
{
    $CFCountry = $_SERVER['HTTP_CF_IPCOUNTRY'];

    switch ($CFCountry){
        case "SE":
            wp_redirect( 'https://tidanapp.com/shop/sv/' );
            exit;   
    }
}

您可能正在寻找以下内容:

add_filter( 'after_setup_theme', 'ip_redirect_on_cloudflare', 1);
function ip_redirect_on_cloudflare()
{
    $current_country = ''; // get current country from your multilanguage plugin
    $CFCountry = $_SERVER['HTTP_CF_IPCOUNTRY'];

    if(strtolower($current_country) != strtolower($CFCountry)) {
      switch ($CFCountry){
        case "SE":
            wp_redirect( 'https://tidanapp.com/shop/sv/' );
            exit;   
      }
    }
}
或者,如果您无法从正在使用的插件中获取当前国家/语言,您可以根据URL进行检查(这不是最好的选项):


那么,当这个国家是
SE
时会发生什么呢?它重定向。此代码是否也在
/sv/
版本上调用?为什么在这种情况下不应该发生重定向?尝试使用break而不是exit函数。这样你就不会毫无意义地绕着一个圈转了。
add_filter( 'after_setup_theme', 'ip_redirect_on_cloudflare', 1);
function ip_redirect_on_cloudflare()
{
    $CFCountry = $_SERVER['HTTP_CF_IPCOUNTRY'];

    switch ($CFCountry){
      case "SE":
          if(strpos($_SERVER['REQUEST_URI'], '/sv/') === false) {
              wp_redirect( 'https://tidanapp.com/shop/sv/' );
          }
            exit;   
    }
}