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

发布于 2024-09-15 06:51:57 字数 225 浏览 1 评论 0原文

我有一个包含以下内容的网址:

/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

纵山崖 2024-09-22 06:51:58

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

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

mod_rewrite 测试您的 URL 时,它们已被解码为其自然字符格式,因此在case %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 在以前的 Apache 版本的 mod_rewrite 中可用作内部映射,但不幸的是,必须在以下位置启用此映射服务器或虚拟服务器配置级别,因此如果您在共享主机上运行站点,则可能不可用。它做同样的事情,尽管更刻意一些。

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

RewriteMap escape int:escape

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

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

请记住,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:

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.

兮颜 2024-09-22 06:51:58

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/

作业与我同在 2024-09-22 06:51:58

在 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)))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文