PHP Mod_rewrite 和 URL 编码符号 - 只能使用其中一个,但不能同时使用两者?
mod_rewrite 似乎在将加号转换为 $_REQUEST 之前将其转换为 $_REQUEST,并且我不知道如何修复它...
RewriteRule ^invite/([a-zA-Z0-9\-\+\/]+)/?$ invite.php?key=$1 [L,QSA]
例如,我将其输入到我的 URL 中,
http://mywebsite/invite/xPo8lUEXpqg8bKL%2B32o6yIOK
我得到了这个,
xPo8lUEXpqg8bKL 32o6yIOK
但是如果我输入此请求不通过 mod_rewrite,
http://mywebsite/invite.php?key=xPo8lUEXpqg8bKL%2B32o6yIOK
我得到了我想要的,
xPo8lUEXpqg8bKL+32o6yIOK
我能做什么?或者我只能使用它们中的一个而不能同时使用它们?
谢谢。
The mod_rewrite seems to convert the symbol of plus before I get it into $_REQUEST, and I don't know what to fix it...
RewriteRule ^invite/([a-zA-Z0-9\-\+\/]+)/?$ invite.php?key=$1 [L,QSA]
For instance, I input this into my URL,
http://mywebsite/invite/xPo8lUEXpqg8bKL%2B32o6yIOK
i get this,
xPo8lUEXpqg8bKL 32o6yIOK
but if I input this request without passing through the mod_rewrite,
http://mywebsite/invite.php?key=xPo8lUEXpqg8bKL%2B32o6yIOK
i get this what I want,
xPo8lUEXpqg8bKL+32o6yIOK
What can I do? Or is it that I only can use either them but not both??
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试添加 [B] 标志(转义反向引用):
Try adding the [B] flag (escape backreferences):
“+”字符在 URL 的查询字符串部分中保留为空格。实际上,“+”作为保留字符的状态记录在 rfc3986 及其(现在旧版)用作空格替换字符记录在 rfc1630 中。
由于 Apache 试图避免任何冲突,因此在传递之前它会自动将“+”作为字符串转义。
在重写中使用
[NE] (NoEscape)
标志应该可以防止这种行为发生。但是,通过使用此功能,如果用户手动键入 URL,未转义的“+”将被空格替换。为了安全起见,只需将输入中的所有空格替换为“+”号即可。
坦率地说,由于您的输入不接受空格,因此只需将所有空格替换为“+”符号即可。使用
[NE]
标志可能会带来比简单的字符替换更大的问题。一个简单的$_GET['key'] = str_replace($_GET['key'], ' ', '+');
就足够了。The "+" character is reserved in the query string part of an URL as a space. Actually, "+" status as a reserved character is documented in rfc3986 and its (now legacy) use as a space replacement character is documented in rfc1630.
Since Apache tries to avoid any conflict, it automatically escapes "+" as a string before passing it over.
Using the
[NE] (NoEscape)
flag on your rewrite should prevent that behavior from happening.However, by using this, unescaped "+" WILL be replaced by a space if the user types the URL manually. To be on the safe side, simply replace all spaces in your input by "+" signs.
Quite frankly, since you do not accept spaces in your input, simply replace all spaces with a "+" symbol. Using the
[NE]
flag can bring out bigger issues then a simple character substitution. A simple$_GET['key'] = str_replace($_GET['key'], ' ', '+');
should suffice.