带有 ../ 的 URL 是如何格式化的?

How are URLs with ../ formatted?

我试验过 URL 这种格式:

https://A.com/B/../C

据我所知,它们总是重新格式化:

https://A.com/B/../C becomes https://A.com/C
https://A.com/B/C/../C/D becomes https://A.com/B/C/D

这是 URL 标准的一部分,还是取决于浏览器或服务器?

这是您可以尝试的示例: https://whosebug.com/questions/ask/../ask/../../questions/ask

是的,它是 RFC 3986

的一部分

path definition我们可以读到

The path segments "." and "..", also known as dot-segments, are defined for relative reference within the path name hierarchy. They are intended for use at the beginning of a relative-path reference (Section 4.2) to indicate relative position within the hierarchical tree of names.

并且later我们可以看到这些相关段是如何处理的,

             [...]
              else
                 T.path = merge(Base.path, R.path);
                 T.path = remove_dot_segments(T.path);
              endif;
             [...]

以及相对部分../C如何将mergedbasehttps://A.com/B变成https://A.com/../C

return a string consisting of the reference's path component appended to all but the last segment of the base URI's path (i.e., excluding any characters after the right-most "/" in the base URI path, or excluding the entire base URI path if it does not contain any "/" characters).

以及点 /../ 将如何得到 replaced by /

[...]

  • C. if the input buffer begins with a prefix of "/../" or "/..", where ".." is a complete path segment, then replace that prefix with "/" in the input buffer and remove the last segment and its preceding "/" (if any) from the output buffer; [...]

所以我们可以得到最后的https://A.com/C