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

自闭症网瘾萝莉.ら 提交于 2019-12-12 08:23:34

问题


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 using PHP?


回答1:


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




回答2:


original uri should be in $_SERVER['REQUEST_URI']



来源:https://stackoverflow.com/questions/6588661/php-how-can-i-get-the-url-that-has-been-rewritten-with-mod-rewrite

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!