为什么url-encoding在域破坏url后第一个斜杠? [英] Why Does url-encoding the first slash after the domain break the url?
问题描述
Salvete!我发现某种url编码方式会破坏链接。对于记录%2f
表示正斜杠字符: /
Salvete! I have discovered that a certain way of url encoding breaks the link. For the record %2f
represents the forward slash character: /
现在,考虑一下:
原始链接: http://dottech.org/95285/this-is-the-pacific-barreleye-a-fish-with-a-transparent -head-amazing-photo-of-day
javascript( encodeURIComponent
)urlencoded链接: http://dottech.org%2f95285%2fthis-is-the-pacific-barreleye-a-fish-with-a-transparent-head-amazing-photo-of-the-一天
javascript (encodeURIComponent
) urlencoded link: http://dottech.org%2f95285%2fthis-is-the-pacific-barreleye-a-fish-with-a-transparent-head-amazing-photo-of-the-day
现在,如果您将编码的链接粘贴到浏览器的地址栏中,它就会被破坏(Firefox,Chrome,IE)。
Now, if you paste the encoded link into your browser's address bar, it is broken (Firefox, Chrome, IE).
但是,如果你没有对第一个正斜杠进行网址编码,它就能完美运行:
' http://dottech.org/95285%2fthis-is-the-pacific-barreleye-a-fish-with-a-transparent-head-amazing-photo-of-the-day
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
为什么?
推荐答案
/
是一个保留字符。它的不相当于%2f
。如果您需要斜杠而不其定义的含义,则使用编码形式。
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.
参见 RFC 3986:保留字符:
保留字符的目的是提供一组分隔的
字符,这些字符可与URI中的其他数据区分开来。
使用
对应的百分比编码八位字节替换保留字符的URI不相等。编码保留字符的百分比 -
,或解码对应于保留字符的百分比编码八位字节
,将改变大多数应用程序解释URI的
的方式。
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.
如果您不使用保留字符 /
,则提及URL的原因仍然有效第二个斜杠:他们的CMS只是在URL中查找ID部分。因此,您可以向网址添加任何内容,例如以下应该仍然有效:
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
(但是,它似乎仍然必须是在他们的情况下, /
或%2f
。)
(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
这篇关于为什么url-encoding在域破坏url后第一个斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!