mod_rewrite 将所有 & 符号实例替换为 %26 以供稍后使用
我不想在 URL 中使用 & 符号,这样当请求文件时我可以将 & 符号进一步传递到我的系统中。问题是 Apache 的处理方式不同。我不知道
我如何将请求的文件重写为index.php?url=$1,这样我就可以看到它是什么,但如果其中有一个&符号,它就无法继续过去!
如何转义 & 符号或将其转换为十六进制等于 (%26)?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>`
I want to simply not use ampersand in my URL so I can pass ampersands further down into my system when a file is requested. The problem is Apache deals with it differently. I don't know how
I already rewrite the requested file to index.php?url=$1 so I can see what it was, but if it has an ampersand in there, it can't continue past it!
how can I escape the ampersand or turn it into it's hex equal (%26)?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先确保您正在构建的 url 已转义 &符号更改为 %26,因此当它到达您的服务器时它不是 & 符号。然后在你的重写规则中添加 B 标签,这样它就不会将其转换回 &:
RewriteRule ^(.*)$ index.php?url=$1 [B,QSA,L]
这个 B 标签在之后救了我的屁股2天尝试不同的重写规则。
1st make sure the url's you're building have escaped the & symbol to %26 so it's not an ampersand when it gets to your server. then in your rewrite rule add the B tag so it won't convert it back to the &:
RewriteRule ^(.*)$ index.php?url=$1 [B,QSA,L]
This B tag saved my butt after 2 days of trying different rewrite rules.
在由
mod_rewrite
修改的查询字符串中转义&符号的正确方法是如果它由 php 处理。
当apache重写它时,它从
%2526
变成%26
,然后php将其转换为&
并存储在查询变量中。使用
B
重写规则也有效,但是参数中的所有加号在 php 中仍然是加号。我必须面对的现实世界的例子:
使用
B
它会重写并在 php 中,
当你想要
使用 B 时,你需要
它,它工作得很好。
如果没有
B
你会得到并最终得到,
因为
&
划分了查询字符串。但如果你双重转义,
它会重写为
,最终会得到
预期的结果。
如果
mod_rewrite
没有将 url 解析为“干净的 url”,那么我们可以使用%26
就可以了。The proper way to escape ampersand in a query string modified by
mod_rewrite
isif it's being handled by php.
When apache rewrites it, it's turned from
%2526
to%26
which php then converts into&
and stores in the query variable.Using the
B
rewrite rule works too, but then all plus signs in the parameter remain plus signs in php.The real world example that I had to face:
With
B
it rewrites toand in php you get
when you wanted
With B you need
which works fine.
without
B
you getand you end up with
because the
&
divides query strings.But if you double escape
it rewrites to
and you end up with
like expected.
If the url is NOT parsed by
mod_rewrite
into a "clean url", then we can use%26
just fine.添加
到您的重写规则。
Add
To your rewrite rules.
尝试将此规则放在您的规则之上:
Try putting this rule above yours:
此外,如果您想向浏览器发送 URL 中带有“&”符号的重定向,您可以将 [NE] 标志添加到 RewriteRule 的末尾。
In addition if you want to send back to the browser a redirect with the ampersand in the URL you can add the [NE] flag to the end of the RewriteRule.