【发布时间】:2012-07-06 21:23:49
【问题描述】:
为什么在使用干净的 url 时没有将查询字符串添加到 GET 数组中?例如。使用/foo/bar?stack=overflow 且$_GET['stack'] 变量为空。
我正在用这段代码实现干净的 url:
$request = rawurldecode($_SERVER['REQUEST_URI']);
// Extracts the index.php file path (eg. /my_app)
// There is no need to specify the path to index.php as the script handles it
$path_to_index = str_ireplace($_SERVER['DOCUMENT_ROOT'].'/', '', $_SERVER['SCRIPT_FILENAME']);
$path_to_index = str_ireplace('/'.basename($_SERVER['SCRIPT_FILENAME']), '', $path_to_index);
// Get rid of the path to index.php
$request = str_ireplace($path_to_index, '', $request);
// Get rid of index.php in the URL
$request = str_ireplace(basename($_SERVER['SCRIPT_FILENAME']), '', $request);
// Our URL is now clean so we can explode
$request = explode('/', $request);
// Delete empty and reindex
$request = array_values(array_filter($request));
【问题讨论】:
-
如何获得干净的 url 效果?
-
您是否在 .htaccess 文件中使用 URL 重写?
-
这取决于你是如何实现 URL 重写的,不是吗?
标签: php query-string clean-urls