PHP:干净的URL和查询字符串

时间:2012-07-06 21:23:50

标签: php query-string clean-urls

使用干净网址时,为什么查询字符串没有添加到GET数组?例如。使用/foo/bar?stack=overflow$_GET['stack']变量为空。

我正在使用以下代码实现干净的网址:

$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)); 

3 个答案:

答案 0 :(得分:2)

如果在.htaccess中使用RewriteRules

RewriteRule行的末尾添加[QSA],如下所示:

RewriteRule (.*) index.php?stuff=$1 [QSA]

QSA标志信息:http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_qsa

答案 1 :(得分:0)

也许这是b / c你没有使用$ _GET? GET不是变量,但应该可以正常工作......

答案 2 :(得分:0)

你probalby忘记在全局变量名中使用下划线(“_”)。

尝试使用$ _GET ['stack']。

相关问题