URL长度限制的历史渊源与现代标准
在调试支付宝接口过程中,注意到其请求URL的长度显著超出以往认知的255个字符限制。为此进行了详细搜索与验证,梳理出以下关键信息:
关于“URL不得超过255字节”的说法确有渊源,该观点源自RFC2616标准文档中的相关描述:
复制代码 代码如下: The HTTP protocol does not place any a priori limit on the length of a URI. Servers MUST be able to handle the URI of any resource they serve, and SHOULD be able to handle URIs of unbounded length if they provide GET-based forms that could generate such URIs. A server SHOULD return 414 (Request-URI Too Long) status if a URI is longer than the server can handle (see section 10.4.15).
Note: Servers ought to be cautious about depending on URI lengths above 255 bytes, because some older client or proxy implementations might not properly support these lengths.
由此可见,255字节的限制主要是为了兼顾旧版客户端或代理服务器的兼容性。实际上,当前主流浏览器的实际限制如下所示:
复制代码 代码如下: Microsoft Internet Explorer (Browser) Microsoft states that the maximum length of a URL in Internet Explorer is 2,083 characters, with no more than 2,048 characters in the path portion of the URL. In my tests, attempts to use URLs longer than this produced a clear error message in Internet Explorer.
Firefox (Browser) After 65,536 characters, the location bar no longer displays the URL in Windows Firefox 1.5.x. However, longer URLs will work. I stopped testing after 100,000 characters.
Safari (Browser) At least 80,000 characters will work. I stopped testing after 80,000 characters.
Opera (Browser) At least 190,000 characters will work. I stopped testing after 190,000 characters. Opera 9 for Windows continued to display a fully editable, copyable and pasteable URL in the location bar even at 190,000 characters.
Apache (Server) My early attempts to measure the maximum URL length in web browsers bumped into a server URL length limit of approximately 4,000 characters, after which Apache produces a “413 Entity Too Large” error. I used the current up to date Apache build found in Red Hat Enterprise Linux 4. The official Apache documentation only mentions an 8,192-byte limit on an individual field in a request.
Microsoft Internet Information Server The default limit is 16,384 characters (yes, Microsoft's web server accepts longer URLs than Microsoft's web browser). This is configurable.
Perl HTTP::Daemon (Server) Up to 8,000 bytes will work. Those constructing web application servers with Perl's HTTP::Daemon module will encounter a 16,384 byte limit on the combined size of all HTTP request headers. This does not include POST-method form data, file uploads, etc., but it does include the URL. In practice this resulted in a 413 error when a URL was significantly longer than 8,000 characters. This limitation can be easily removed. Look for all occurrences of 16×1024 in Daemon.pm and replace them with a larger value. Of course, this does increase your exposure to denial of service attacks.
此外,有资料指出当URL作为href属性使用时,长度不应超过1024字节,但该说法未进行详细核实。
综上所述,URL并非越长越好。若非必要,应避免通过GET方式传递大量参数,建议改用POST方法(通常可支持约2MB数据,具体取决于服务器配置)。同时,过长的URL在访问和收藏时体验较差,部分浏览器在收藏超长链接时也可能出现异常。因此,此前数据库中按255字节设计的字段可能需要重新评估并扩充。