重写包含问号的 URL

2022-01-04 00:00:00 get php query-string url-rewriting

我遇到了有关 URL 重写的问题.我正在使用 Apache 的 mod rewrite 来重写 URL.比如我改写网址

I am encountering a problem regarding URL rewriting. I am using Apache's mod rewrite to rewrite URLs. For example, I rewrite URL

  • www.website.com/some/path/
  • request.php?string=some/path/.

然后我显示此 URL 的特定响应.现在我的重写规则是这样的:

Then I show specific response for this URL. Right now my rewrite rule looks like this:

RewriteRule ^([a-z_/?]+)$ request.php?string=$1

但是如果我有 URL www.website.com/some/data/?id=12&name=John 并重写它,问题就会开始,我得到这样的东西:request.php?string=some/data/?id=12&name=John.在这个例子中,另一个问号似乎混淆了 PHP.如果我尝试在 request.php 中检索 $_GET['string'],我得到的只是:some/data/.

But the problem begins if I have URL www.website.com/some/data/?id=12&name=John and rewrite it, I get something like this: request.php?string=some/data/?id=12&name=John. It seems that in this example another question mark confuses PHP. If I try to retrieve $_GET['string'] in request.php all I get is: some/data/.

为了进一步参考,Gmail 对其网址做了类似的处理:
https://mail.google.com/mail/?ui=1&shva=1

For further reference, Gmail does something similar with it's URL:
https://mail.google.com/mail/?ui=1&shva=1

推荐答案

我建议(作为 mario)查看 QSA 标志(查询字符串附加).另外,我会从正则表达式中的字符类中去掉问号:

I suggest (as mario) to take a look into the QSA flag (Query String Append). Additionally I would take the question-mark out of the character class in the regex:

RewriteRule ^([a-z_/]+)$ request.php?string=$1 [L,QSA]

相关文章