Rewriting URL that contains question mark(重写包含问号的 URL)
问题描述
我遇到了有关 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]
这篇关于重写包含问号的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:重写包含问号的 URL


基础教程推荐
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01