admin管理员组

文章数量:1415137

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:

javascript (encodeURIComponent) urlencoded link:

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: '

Why?

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/95285/this-is-the-pacific-barreleye-a-fish-with-a-transparent-head-amazing-photo-of-the-day

javascript (encodeURIComponent) urlencoded link: http://dottech%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/95285%2fthis-is-the-pacific-barreleye-a-fish-with-a-transparent-head-amazing-photo-of-the-day

Why?

Share Improve this question edited Feb 7, 2013 at 0:47 unor 96.9k28 gold badges225 silver badges381 bronze badges asked Jan 31, 2013 at 17:02 bgmCoderbgmCoder 6,3709 gold badges66 silver badges110 bronze badges 2
  • 1 To encode a plete URL use encodeURI- encodeURIComponent encodes pieces of an url location- protocol,host,port,pathname,hash and search – kennebec Commented Jan 31, 2013 at 17:09
  • Related post - Is a slash (“/”) equivalent to an encoded slash (“%2F”) in the path portion of an HTTP URL & How to URL Encode a Backslash with R/RCurl – RBT Commented Jul 10, 2018 at 5:26
Add a ment  | 

1 Answer 1

Reset to default 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/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/wiki%2fStack_Overflow

本文标签: javascriptWhy Does urlencoding the first slash after the domain break the urlStack Overflow