5

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?

unor
  • 92,415
  • 26
  • 211
  • 360
bgmCoder
  • 6,205
  • 8
  • 58
  • 105
  • 1
    To encode a complete URL use encodeURI- encodeURIComponent encodes pieces of an url location- protocol,host,port,pathname,hash and search – kennebec Jan 31 '13 at 17:09
  • Related post - [Is a slash (“/”) equivalent to an encoded slash (“%2F”) in the path portion of an HTTP URL](https://stackoverflow.com/q/1957115/465053) & [How to URL Encode a Backslash with R/RCurl](https://stackoverflow.com/q/26304172) – RBT Jul 10 '18 at 05:26

1 Answers1

5

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
Community
  • 1
  • 1
unor
  • 92,415
  • 26
  • 211
  • 360
  • Why then, do they call it "**url** encoding" whereby the `/` gets encoded to `%2f%`? – bgmCoder Feb 02 '13 at 19:46
  • @BGM: I don't understand what you mean by that. Who calls it URL encoding? The spec talks about percent encoding. – unor Feb 02 '13 at 20:12
  • Well, in the javascript function. The function is called ` encodeURI` and it changes the slash to a token. Maybe I have something backward? – bgmCoder Feb 02 '13 at 23:46
  • @BGM: I don't know JS, but [according to MDN](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURI) `encodeURI` shouldn't encode a `/`. I did a quick test and it worked, the slash stayed being a slash. – unor Feb 03 '13 at 04:43
  • I'm sorry - you are right `encodeURI` does *not* encode the `/` - I meant `encodeURIComponent`. I believe your answer is correct, however. So why does `encodeURIComponent` encode the `/`? Do you know? – bgmCoder Feb 03 '13 at 15:42
  • 2
    @BGM: Because that is [how `encodeURIComponent` is defined](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIcomponent): "you should call encodeURIComponent on any user-entered parameters that will be passed as part of a URI". Users shouldn't probably be able to enter reserved characters. So `encodeURIcomponent` encodes *everything* **except**: alphabetic, decimal digits, `- _ . ! ~ * ' ( )`. – unor Feb 03 '13 at 16:16
  • `%2F` does not have hierarchical significance and thus is not treated as `/`. See Example 2 in [W3C's URI recommendations](https://www.w3.org/Addressing/URL/4_URI_Recommentations.html) - taken from https://stackoverflow.com/a/42487180/873282 – koppor Jul 20 '17 at 18:00