IIS上的PHP获取当前重写的URL

时间:2013-08-20 08:55:13

标签: php iis

无论如何在IIS上用PHP获取当前完整的重写URL。

例如,我有一个像:

这样的网址

http://www.domain.com/searchresults.php?section=99&page=1&model=section-name

改写为: http://www.domain.com/section99/page1/section-name

我已经能够使用以下方法拼凑原始网址:

function selfURL(){
    if(!isset($_SERVER['REQUEST_URI'])){
        $serverrequri = $_SERVER['PHP_SELF'];
    }else{
        $serverrequri =    $_SERVER['REQUEST_URI'];
    }
    $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
    $protocol = "http";
    $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
    return $protocol."://".$_SERVER['SERVER_NAME'].$port.$serverrequri.$_SERVER['QUERY_STRING'];   
}
print(selfURL());

无论如何,我可以轻松选择重写的URL,以避免根据当前页面类型从多种不同格式和变量中计算出友好URL的开销吗?

2 个答案:

答案 0 :(得分:0)

我建议您在.htaccess文件中使用Apache的mod_rewrite rules,然后将其导入IIS 7.这将生成web.config文件。

当然,如果你知道如何直接在web.config上工作,那就去吧!

答案 1 :(得分:0)

我可以使用:

<强> HTTP_X_ORIGINAL_URL

function selfURL(){
    if(!isset($_SERVER['REQUEST_URI'])){
        $serverrequri = $_SERVER['PHP_SELF'];
    }else{
        $serverrequri =    $_SERVER['REQUEST_URI'];
    }
    $protocol = empty($_SERVER["HTTPS"]) ? 'http' : ($_SERVER["HTTPS"] == "on") ? "https" : "http";
    $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
    return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER["HTTP_X_ORIGINAL_URL"];
}
print(selfURL());