gpt4 book ai didi

javascript - 为什么 url 编码域后的第一个斜杠会破坏 url?

转载 作者:搜寻专家 更新时间:2023-11-01 04:10:04 25 4
gpt4 key购买 nike

救命恩人!我发现某种 url 编码方式会破坏链接。备案 %2f 代表正斜杠字符:/

现在,考虑一下:原文链接:http://dottech.org/95285/this-is-the-pacific-barreleye-a-fish-with-a-transparent-head-amazing-photo-of-the-day

javascript (encodeURIComponent) urlencoded 链接:http://dottech.org%2f95285%2fthis-is-the-pacific-barreleye-a-fish-with-a-transparent-令人惊叹的今日头图

现在,如果您将编码后的链接粘贴到浏览器的地址栏中,它就会损坏(Firefox、Chrome、IE)。

但是,如果您不对 first 正斜杠进行 url 编码,它会完美运行:'http://dottech.org/95285%2fthis-is-the-pacific-barreleye-a-fish-with-a-transparent-head-amazing-photo-of-the-day

为什么?

最佳答案

/ 是保留字符。它等同于%2f。如果您需要斜线其定义的含义,您可以使用编码形式。

参见 RFC 3986: "Reserved Characters" :

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

如果您不对第二个斜杠使用保留字符 /,那么 mentionend URL 仍然有效的原因是:他们的 CMS 只是在 URL 中查找 ID 部分。所以你可以添加任何你想要的 URL,例如以下应该仍然有效:

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

(但是,在他们的情况下,似乎仍然必须是/%2f。)

如果您尝试使用维基百科文章,它会重定向到首页:

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

关于javascript - 为什么 url 编码域后的第一个斜杠会破坏 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14631200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com