PHP子域根据语言变量重定向

时间:2014-02-01 01:16:50

标签: php redirect http-redirect

所以我正在尝试编写 php脚本检查语言(由 $ language 中的语言函数定义)对于某个值,如果用户请求任何地址,请访问www.example.com/foo/bar/data.php?=foobar,它会通过* http刷新或重定向重定向 * 或标题位置(不是更可取)到subdomain.example.com/$1($ 1,与原始请求的地址相同)。

类似这样但没有标题位置:

<?php if ($language == "en") { header ("Location: http://"$language".example.com/"$1""); } ?>

这不起作用,我在日志“已经由另一个文件发送的标题”中收到错误“这是我运行的另一个脚本,无法更改代码。

所以,我需要的是一个读取变量的脚本,并根据其值将用户重定向到相应的子域。

希望你能帮助我的家伙,

干杯!

2 个答案:

答案 0 :(得分:0)

<?php
    // Language detection code
    // ...

    if ('en' === $language)
    {
        header('Location: http://' . $language . '.example.com' . $_SERVER['REQUEST_URI']);
        exit();
    }

如果您真的无法更改其他文件中的代码,则需要进行html重定向。

<?php
    // Language detection code
    // ...

    if ('en' === $language)
    {
        $url = 'http://' . $language . '.example.com' . $_SERVER['REQUEST_URI'];
        echo <<<EOF
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Refresh" content="0; url={$url}" />
    </head>
    <body></body>
</html>
EOF;
        exit();
    }

答案 1 :(得分:0)

您好,您可以回显包含重定向的javascript代码。试试这个。

<?php
    if($language === "en"){
        echo "<script type='text/javascript'> document.location = 'http://' . $language . '.example.com/' . $1; </script>";
    }
?>

header('Location: etc...')相比,此代码最适合我。

相关问题