在域之后添加额外路径

时间:2013-04-08 11:32:58

标签: php url redirect

如果尝试在所有网址中的域名后面的URL中添加额外路径

$extra = "en";
$domain = "http://domain.com";
$current = currentURL();
$rest_path = str_replace($domain,'',$current);
$result = $domain."/".$extra.$rest_path;

// $result is "http://domain.com/en/mysub/mysub"

在此之后,我通过使用PHP重定向重定向我的网站

要获取当前网址,就像..

function currentURL() {
    $pageURL=(@$_SERVER["HTTPS"]=="on")?"https://":"http://";
        if($_SERVER["SERVER_PORT"]!="80"){
            $pageURL.=$_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
        }else{
            $pageURL.=$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
        }
        return $pageURL;
}

看起来很多步骤,更容易做到这一点?或者查看我的错误编码。

PS:尽量不要使用.htaccess

2 个答案:

答案 0 :(得分:2)

我会用:

$extra = "en";
$domain = "http://domain.com";
$result = $domain."/".$extra.$_SERVER['REQUEST_URI'];

因为您没有使用协议或域名。

答案 1 :(得分:1)

$extra = 'en';
$domain = $_SERVER['SERVER_NAME'].'/'.$_SERVER['REQUEST_URI'];
$new_url=str_replace('.com','.com/'.$extra,$domain);

没有