Apache:url 中的 %25(400 错误请求)
我有一个包含以下内容的网址:
/somepath/morestuff/ohno%25foobar
出于某种原因,apache 报告了 400 错误请求(它与 %25 有关)。我正在使用 mod_rewrite 重写指向我的 codeigniter 实例的路径,但它甚至没有到达 codeigniter,这只是默认的 apache 错误。
有什么想法吗?
I have a url containing the following:
/somepath/morestuff/ohno%25foobar
For some reason apache is reporting a 400 bad request (it has something to do with the %25). I am using mod_rewrite to rewrite the path to point to my codeigniter instance but it's not even getting to codeigniter, it's just the default apache error.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑您正在使用
PATH_INFO
来处理您的 CodeIgniter 请求。因此,您的.htaccess
文件包含一个类似于以下内容的规则集:当
mod_rewrite
测试您的 URL 时,它们已被解码为其自然字符格式,因此在case%25
已变为%
。当您应用此规则时,反向引用实际上包含文字文本somepath/morestuff/ohno%foobar
,默认情况下不会重新编码。 Apache 不知道%
在/index.php/somepath/morestuff/ohno%foobar
的请求路径中做了什么,并且阻塞,给你这个错误。如果您运行的是 Apache 2.2,
mod_rewrite
为此添加了B
标志,允许您自动转义重写到 URL 的反向引用。将其添加到当前的标志列表应该可以解决这种情况下的问题:还有一个
escape
RewriteMap
在以前的 Apache 版本的mod_rewrite
中可用作内部映射,但不幸的是,必须在以下位置启用此映射服务器或虚拟服务器配置级别,因此如果您在共享主机上运行站点,则可能不可用。它做同样的事情,尽管更刻意一些。在您的服务器/虚拟服务器配置中:
然后,无论您在何处定义规则:
请记住,CodeIgniter 不需要使用
PATH_INFO
来获取请求信息,而是使用REQUEST_URIAUTO
获取请求(假设我没有混淆我的框架),因此只需不将请求重写为带有路径信息的 URL 就足以使那个改变。I suspect that you're using
PATH_INFO
to handle your CodeIgniter requests. Consequently, your.htaccess
file contains a rule set that looks similar to this:When
mod_rewrite
tests your URLs, they have already been decoded to their natural character format, so in this case%25
has become just%
. When you apply this rule, the backreference actually contains the literal textsomepath/morestuff/ohno%foobar
, which is not re-encoded by default. Apache has no idea what that%
is doing in your request path to/index.php/somepath/morestuff/ohno%foobar
and chokes, giving you that error.If you're running Apache 2.2,
mod_rewrite
added theB
flag for this purpose, allowing you to automatically escape backreferences rewritten to your URL. Adding it to your current flag list should fix the problem in that case:There's also an
escape
RewriteMap
that's available as an internal map in previous Apache versions ofmod_rewrite
, but unfortunately this map has to be enabled at the server or virtual server configuration level, so may not be available if you're running your site on shared hosting. It does the same thing, though a bit more deliberately.In your server/virtual server configuration:
Then, wherever you define your rules:
Keep in mind that CodeIgniter doesn't need to use
PATH_INFO
to get the request information, and usingREQUEST_URI
is perfectly acceptable here if you aren't usingmod_rewrite
to do any other transformations (and would avoid this headache altogether). I think by default CodeIgniter is set to get the request fromAUTO
(assuming I haven't gotten my frameworks mixed up), so simply not rewriting the request to the URL with path info would be enough to make that change.Apache 2.2.12+,只需使用
B
标志。否则,请参见此处:http://www.dracos.co.uk/代码/apache-rewrite-问题/
Apache 2.2.12+, just use the
B
flag. Otherwise, see here:http://www.dracos.co.uk/code/apache-rewrite-problem/
在 jquery 中,在设置 url 之前,您必须像这样进行编码
这将对 % 和 / 两者进行编码......
在 php 或视图页面上的“encodeURI(encodeURI(encodeURIComponent('your string')));”,
您必须像下面这样进行解码
urldecode(urldecode(htmlspecialchars('您的编码字符串', ENT_QUOTES) )))
In jquery before setting to url you have to encode like this
this will encode % and / both.....
"encodeURI(encodeURI(encodeURIComponent('your string')));"
on php or view page you have to decode like following
urldecode(urldecode(htmlspecialchars('your encoded string', ENT_QUOTES)))