문제

I'm trying to write a php script that checks the language(which is defined by the language function in $language) for a value and if user requests any address, www.example.com/foo/bar/data.php?=foobar it will redirect him by http refresh or redirect or header location(not preferable) to subdomain.example.com/$1 ($1 as in the same original requested address).

something like this but without the header location:

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

this does not work, also I get an error in the log "header already sent by another file" which is another script I got running and cannot change the code.

So, what I need is a script that reads the variable and according to its value it will redirect the user to the appropriate subdomain.

도움이 되었습니까?

해결책

Hi you can echo a javascript code that contains redirection. Try this one.

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

This code works most for me compared to header('Location: etc...').

다른 팁

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

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

If you really can't change the code from the other file, you'll need to make an html redirection instead.

<?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();
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top