Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 607
bgmCoder Avatar asked Jan 31 '13 17:01

bgmCoder


1 Answers

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
like image 171
unor Avatar answered Sep 23 '22 16:09

unor