Why Does url-encoding the first slash after the domain break the url?

我是研究僧i 提交于 2019-12-01 17:14:10

问题


Salvete! I have discovered that a certain way of url encoding breaks the link. For the record %2f represents the forward slash character: /

Now, consider this: Original Link: http://dottech.org/95285/this-is-the-pacific-barreleye-a-fish-with-a-transparent-head-amazing-photo-of-the-day

javascript (encodeURIComponent) urlencoded link: http://dottech.org%2f95285%2fthis-is-the-pacific-barreleye-a-fish-with-a-transparent-head-amazing-photo-of-the-day

Now, if you paste the encoded link into your browser's address bar, it is broken (Firefox, Chrome, IE).

However, if you don't url-encode the first forward slash, it works perfectly: 'http://dottech.org/95285%2fthis-is-the-pacific-barreleye-a-fish-with-a-transparent-head-amazing-photo-of-the-day

Why?


回答1:


The / is a reserved character. It’s not equivalent to %2f. If you need the slash without its defined meaning, you’d use the encoded form.

See RFC 3986: "Reserved Characters":

The purpose of reserved characters is to provide a set of delimiting characters that are distinguishable from other data within a URI. URIs that differ in the replacement of a reserved character with its corresponding percent-encoded octet are not equivalent. Percent- encoding a reserved character, or decoding a percent-encoded octet that corresponds to a reserved character, will change how the URI is interpreted by most applications.

The reason why the mentionend URL still works if you don’t use the reserved char / for the second slash: their CMS simply looks for the ID part in the URL. So you can add whatever you want to the URL, e.g. the following should still work:

http://dottech.org/95285/hey-this-URL-got-featured-at-stackoverflow

(However, it seems that it still has to be / or %2f in their case.)

If you try it with a Wikipedia article, it redirects to the front page:

http://en.wikipedia.org/wiki%2fStack_Overflow


来源:https://stackoverflow.com/questions/14631200/why-does-url-encoding-the-first-slash-after-the-domain-break-the-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!