PHP: How can I get the URL that has been rewritten with mod_rewrite?

后端 未结 3 688
野的像风
野的像风 2021-02-19 09:15

For example, if I rewrite /category/topic/post/ to /index.php?cat=1&topic=2&post=3, how can I get /index.php?cat=1&topic=2&post=3

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-19 09:55

    You can recreate it pretty easily. $_SERVER['PHP_SELF'] will still give you the correct file name for the script. This should do the trick:

    $url = $_SERVER['PHP_SELF'];
    $parts = array();
    foreach( $_GET as $k=>$v ) {
        $parts[] = "$k=" . urlencode($v);
    }
    
    $url .= "?" . implode("&", $parts);
    

    $url will now be the URL you're looking for.

    EDIT: @carpereret's answer is far better. Upvote him instead

提交回复
热议问题