【问题标题】:PHP: clean urls and query stringsPHP:干净的 url 和查询字符串
【发布时间】: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


【解决方案1】:

如果在 .htaccess 中使用 RewriteRules

在您的RewriteRule 行的末尾输入[QSA],如下所示:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 好吧,很抱歉省略了“$_”。当然我的意思是 $_GET['stack]。我以为你们会填补空白。
    • 很明显应该可以工作,也许我会改写这个问题,因为查询参数通常与“漂亮的网址”无关;也许像“$_GET 不工作,怎么办?”之类的东西?
    【解决方案3】:

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

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

    【讨论】:

    • 我省略了“$_”。我以为你们会填补空白。然后我又愚蠢地把你们放在心上的东西……
    猜你喜欢
    • 2012-08-26
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多