php - 为什么带有重定向的 add_filter 会导致无限重定向循环?
问题描述
我可以看到下面的代码有问题,但不知道为什么会导致重定向循环?
// 根据 Location_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 进行检查(这不是最好的选择):
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;
}
}
推荐阅读
- swift - UILabel 值不会根据 pickerView 选择而改变
- css - 如何在浏览器扩展中注入带有用户来源的 CSS?
- javascript - Swift 5 - 如何将 LONGBLOB/Buffer 转换为数据
- go - 如何在表面上画一条线?
- python - 正则表达式从最后一个连字符中提取前 5 个数字+字符
- python - 使用 matplotlib 的 3D 动画来可视化移动的简笔画
- php - 将数组从 PHP 传递到 PowerShell
- javascript - AFrame 为每个对象更新 material.offset
- javascript - 我在会话中做错了什么?
- php - 如何随机化 json