您如何配置 Apache/PHP 以接受查询字符串中的斜杠?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/462082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you configure Apache/PHP to accept slashes in query strings?
提问by Jason Cohen
I have two Apache servers running PHP. One accepts forward-slashes in the query string and passes it along to PHP in the expected way, for example:
我有两个运行 PHP 的 Apache 服务器。接受查询字符串中的正斜杠并以预期的方式将其传递给 PHP,例如:
http://server/index.php?url=http://foo.bar
works and in PHP this expression is true:
有效并且在 PHP 中此表达式为真:
$_REQUEST['url'] == "http://foo.bar"
However, in the otherApache server, the same URL results in a 403 Forbiddenerror! Note that if the query string is properly URL-escaped (i.e. with %2Finstead of forward-slash), then everything works.
但是,在其他Apache服务器中,同样的URL导致403 Forbidden错误!请注意,如果查询字符串是正确的 URL 转义(即使用%2F而不是正斜杠),则一切正常。
Clearly there's some difference in the Apache or PHP configuration that causes this, but I can't figure out what!
很明显,导致这种情况的 Apache 或 PHP 配置存在一些差异,但我不知道是什么!
I want to acceptthis form of URL in both cases, not reject it.
我想在两种情况下都接受这种形式的 URL,而不是拒绝它。
采纳答案by troelskn
http://server/index.php?url=http://foo.baris not a valid url. You have to encode the slashes. I think browsers do this automagically, so maybe you were testing with different browsers?
http://server/index.php?url=http://foo.bar不是有效的网址。您必须对斜杠进行编码。我认为浏览器会自动执行此操作,所以也许您正在使用不同的浏览器进行测试?
Or perhaps it's the AllowEncodedSlashessetting?
或者它是AllowEncodedSlashes设置?
回答by Alex
A few posts here suggest the OP's usage is wrong, which is false.
这里的一些帖子表明 OP 的用法是错误的,这是错误的。
Expanding on Sam152's comment, query strings are allowed to contain both ? and / characters, see section 3.4 of http://www.ietf.org/rfc/rfc3986.txt, which is basically the spec written by Tim Berners-Lee and friends governing how the web should operate.
扩展 Sam152 的评论,查询字符串可以同时包含 ? 和 / 字符,请参阅http://www.ietf.org/rfc/rfc3986.txt 的第 3.4 节,这基本上是 Tim Berners-Lee 和他的朋友编写的规范网络应该如何运行的规范。
The problem is that poorly written (or poorly configured, or misused) parsers interpret query string slashes as separating path components.
问题在于编写不当(或配置不当或误用)的解析器将查询字符串斜杠解释为分隔路径组件。
I have seen examples of PHP's pathinfo function being used to parse URL's. Pathinfo wasn't written to parse a URL. You can however extract the path using parse_url then use fileinfo to retrieve details from the path. You will see that parse_url handles / and ? in query strings just fine.
我已经看到 PHP 的 pathinfo 函数用于解析 URL 的示例。Pathinfo 不是用来解析 URL 的。但是,您可以使用 parse_url 提取路径,然后使用 fileinfo 从路径中检索详细信息。你会看到 parse_url 处理 / 和 ? 在查询字符串中就好了。
In any case, the overall problem is that this area is poorly understood all-round, even among experienced developers, and most people (myself included until recently) just assume that anything after the filename has to be urlencoded, which is patently false if you take the standards into consideration.
无论如何,总体问题在于,即使是有经验的开发人员,对这个领域的全面了解也很少,而且大多数人(包括我自己直到最近)只是假设文件名之后的任何内容都必须进行 urlencoded,如果您这样做,这显然是错误的考虑标准。
tl;dr Read the spec :)
tl;dr 阅读规范 :)
回答by Ian
In your Apache config:
在您的 Apache 配置中:
AllowEncodedSlashes On
See the documentation for more information:
http://httpd.apache.org/docs/2.2/mod/core.html#allowencodedslashes
有关更多信息,请参阅文档:http:
//httpd.apache.org/docs/2.2/mod/core.html#allowencodedslashes
Edit: Hmm, this may be what you already have working... I had this same problem, and what ended up fixing it for me was to just use $_SERVER['REQUEST_URI']as that had the data I needed.
编辑:嗯,这可能是你已经在工作的......我遇到了同样的问题,最终为我解决的问题只是使用$_SERVER['REQUEST_URI']我需要的数据。
回答by Ian
Do you have mod_securityinstalled? See this thread:
你mod_security安装了吗?看到这个线程:
403 Forbidden on PHP page called with url encoded in a $_GET parameter
回答by jmucchiello
Note that if the query string is properly URL-escaped (i.e. with %2F instead of forward-slash), then everything works.
请注意,如果查询字符串是正确的 URL 转义(即使用 %2F 而不是正斜杠),则一切正常。
So it works when the query string is properly formatted and doesn't work when it isn't. What's the problem?
因此,当查询字符串格式正确时它会起作用,而当它不是时则不起作用。有什么问题?
回答by UberDragon
This sounds like another case of default magic_quotes_gpc. On the server causing problems check the php.ini and make sure that
这听起来像是默认 magic_quotes_gpc 的另一种情况。在导致问题的服务器上检查 php.ini 并确保
magic_quotes_gpc = Off
回答by OIS
You dont specify what PHP does with this url. Does it redirect to this page or try to read it?
您没有指定 PHP 对这个 url 做什么。它是重定向到此页面还是尝试阅读它?
There is probably some mod_rewrite rule to remove double slashes, or for some other purpose, which tries to redirect this to somewhere it should not.
可能有一些 mod_rewrite 规则来删除双斜杠,或者出于其他目的,试图将其重定向到它不应该的地方。
Maybe a regex without ^ before http://
可能是在 http:// 之前没有 ^ 的正则表达式

