且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Apache:url 中的 %25(400 错误请求)

更新时间:2022-10-15 22:55:08

我怀疑您正在使用 PATH_INFO 来处理您的 CodeIgniter 请求.因此,您的 .htaccess 文件包含一个类似于以下内容的规则集:

RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d重写规则 ^.*$ index.php/$0 [L]

mod_rewrite 测试你的 URL 时,它们已经被解码为它们的自然字符格式,所以在这种情况下 %25 变成了 %.当您应用此规则时,反向引用实际上包含文字文本 somepath/morestuff/ohno%foobar,默认情况下重新编码.Apache 不知道 % 在您对 /index.php/somepath/morestuff/ohno%foobar 的请求路径中做了什么并阻塞,给您那个错误.>

如果您运行的是 Apache 2.2,mod_rewrite 为此添加了 B 标志,允许您自动转义重写到您的 URL 的反向引用.在这种情况下,将它添加到您当前的标志列表应该可以解决问题:

RewriteRule ^.*$ index.php/$0 [B,L]

还有一个 escape RewriteMap 在以前的 mod_rewrite Apache 版本中可用作内部映射,但不幸的是,必须在服务器或虚拟服务器配置级别启用此映射,因此可能不会如果您在共享主机上运行您的网站,则可用.它做同样的事情,但更刻意.

在您的服务器/虚拟服务器配置中:

RewriteMap 转义 int:escape

然后,无论您在何处定义规则:

RewriteRule ^.*$ index.php/${escape:$0} [L]

请记住,CodeIgniter 不需要使用 PATH_INFO 来获取请求信息,如果您不使用 在这里使用 REQUEST_URI 是完全可以接受的>mod_rewrite 进行任何其他转换(并完全避免这种头痛).我认为默认情况下 CodeIgniter 设置为从 AUTO 获取请求(假设我没有混淆我的框架),所以简单地不使用路径信息将请求重写到 URL 就足够了那个变化.

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?

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:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php/$0 [L]

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 text somepath/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 the B 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:

RewriteRule ^.*$ index.php/$0 [B,L]

There's also an escape RewriteMap that's available as an internal map in previous Apache versions of mod_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:

RewriteMap escape int:escape

Then, wherever you define your rules:

RewriteRule ^.*$ index.php/${escape:$0} [L]

Keep in mind that CodeIgniter doesn't need to use PATH_INFO to get the request information, and using REQUEST_URI is perfectly acceptable here if you aren't using mod_rewrite to do any other transformations (and would avoid this headache altogether). I think by default CodeIgniter is set to get the request from AUTO (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.