【问题标题】:URL redirection not working properly: genetates the url http://domain.com/domain.com/URL 重定向无法正常工作:生成 url http://domain.com/domain.com/
【发布时间】:2012-11-29 09:27:08
【问题描述】:

我有一个使用两种语言(希伯来语和英语)的 WordPress 网站,我需要它根据浏览器语言进行重定向。我正在使用 qTranslate 插件来创建两种语言的内容。这个插件也有重定向功能,但它只为主页创建重定向,我需要为内部页面和主页进行重定向。

另一位开发人员为我编写了这段代码来创建重定向,但由于某种原因,它创建了一个有趣的重定向。仅在将语言切换为希伯来语,然后离开站点并尝试直接进入 http://domain.com/en/ 时才会发生这种情况,它会将您重定向到 http://domain.com/domain.com/(切换到英语时不会发生)。

我尝试使用为希伯来语创建重定向的“标题(位置:)”,但无法弄清楚如何使其工作 - 我尝试使用完整路径而不是相对路径,或删除“/ " 在 $_SERVER['SERVER_NAME']$_SERVER['REQUEST_URI'] 之间,但得到递归 url 或带有双“/”的 url(http://domain.com// 以及内部页面 http://domain.com//page)。

url结构为:

  • domain.com/ 用于希伯来语
  • domain.com/en/ 英文版

当切换语言时,会添加参数 $lang=en 或 $lang=he。

希望这是有道理的,非常感谢!

这是负责重定向的代码:

<?php
if (!isset($_COOKIE["uln"])) : 
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
setcookie('uln', $lang, time()+86400*365, '/', '.domain.com'); // cookie stored for a year
$_COOKIE['uln'] = $lang;
endif;

//if lang=(value) is not empty 

if(isset($_GET['lang'])) {
$lang = $_GET['lang'];

 setcookie('uln', $lang, time()-1, '/', '.domain.com');  //this unsets the cookie for random language selection

 //set the cookie "uln" again with the selected language.
 setcookie('uln', $lang, time()+86400*365, '/', '.domain.com'); // cookie stored for a year 
 $_COOKIE['uln'] = $lang;
}



        if(($_COOKIE["uln"]) == "en") {
        $matched = strncmp("/en/", $_SERVER['REDIRECT_URL'], 3);                               
        if ($matched !== 0) :       
        header('Location: /en'.$_SERVER['REQUEST_URI']);        
        endif;
   } elseif(($_COOKIE["uln"]) == "he") {
        $matched = strncmp("/en/", $_SERVER['REDIRECT_URL'], 3);                               
        if ($matched === 0) :       
        header('Location: '.$_SERVER['SERVER_NAME'].'/'.$_SERVER['REQUEST_URI']);       
        endif;
   } 

 ?>  

【问题讨论】:

    标签: wordpress redirect multilingual


    【解决方案1】:

    而不是

     header('Location: '.$_SERVER['SERVER_NAME'].'/'.$_SERVER['REQUEST_URI']);       
    

    试试

     header("Location: http://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}");   
    

    URL,尤其是 Location 标头中的 URL,应包含协议和域名。我认为 Location 标头中的相对 URL 违反了 HTTP RFC。

    通过省略协议,您无意中指定了相对 URL,而不是绝对 URL。

    编辑:REQUEST_URI 已经以 / 为前缀,因此不需要在 concat 中包含一个。

    【讨论】:

    • 是的,谢谢。我尝试过这个。它会产生一个带有 // 子库 /en/ 的 URL。我在 htaccess 文件中添加了重写规则以删除双“/”,但这是一个临时解决方案。我希望它能够在没有额外解决方法的情况下正常工作。
    • 可能不需要您在 concat 中包含的“/”。 header("Location: http://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}");。如果您直接使用 curl 或浏览器调试工具检查您的 HTTP 响应,这种事情会很容易看到
    • 太棒了。完美运行!谢谢!
    【解决方案2】:

    您在某处遗漏了 http://,可能在英语 -> 希伯来语重定向代码中。

    改变

    header('Location: '.$_SERVER['SERVER_NAME'].'/'.$_SERVER['REQUEST_URI']);
    

    header('Location: http://'.$_SERVER['SERVER_NAME'].'/'.$_SERVER['REQUEST_URI']);
    

    【讨论】:

    • 谢谢,做到了。看看我给弗兰克写了什么:)
    猜你喜欢
    • 2016-02-20
    • 2016-09-01
    • 2021-06-02
    • 2013-12-07
    • 1970-01-01
    • 2017-09-23
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多