关于URL最大长度限制的相关资料查证
在开发调试支付宝接口时,突然发现支付宝接口的url很长,远远大于之前自己印象中的255个字符。赶紧搜索查证了一番,理解如下:
url不能大于255bytes的说法确实存在,在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.
从上一点也可以看出,255bytes的说法也是为了兼容性考虑。实际上现代浏览器的限制如下:
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.
另外值得注意的是,有文章提到作为<a>的href属性时,url不能超过1024bytes,这点没有详细查证
综上,url还是不适合太长,不是不得已,尽量不要通过get方式提交大量参数,可以考虑用post方式(大约在2m左右,应该是和服务器及设定有关)。另外这么长的url在访问和收藏(有文章提到有些浏览器在收藏超长地址时也是会出现问题)时也是相当不友好的。当然,之前数据库字段设置时还是作为255bytes处理,现在可能要考虑扩充一下了。