【问题标题】:Is a URL with only scheme + path valid?只有方案 + 路径的 URL 是否有效?
【发布时间】:2015-07-15 19:52:07
【问题描述】:

我知道绝对路径 URL (/path/to/resource) 是有效的,并且引用与当前资源相同的方案、主机、端口等。如果添加了相同(或不同!)的方案,该 URL 是否仍然有效? (http:/path/to/resourcehttps:/path/to/resource

如果它根据规范的字母有效,浏览器如何处理它?未来可能会遇到代码的开发人员如何处理它?

附录:

这是我在 Apache 服务器上设置的一个简单测试用例:

resource/number/one/index.html:

<a href="http:/resource/number/two/">link</a>

resource/number/two/index.html:

two

在 OS X 上的 Chrome 43 中进行测试:将鼠标悬停在链接上时显示的 URL 看起来正确。单击链接按预期工作。查看 Web 检查器中的 DOM,将鼠标悬停在 a href URL 上会显示不正确的位置 (/resource/number/one/http:/resource/number/two/)。

Firefox 38 似乎也能正确处理点击。很奇怪。

【问题讨论】:

  • @minitech Chrome 43 似乎可以处理它,尽管这更像是一次偶然的发现,而不是详尽的测试。
  • 我现在正在使用 Chromium 43,它肯定不起作用。 http:/path/to/resource 转到 http://path/to/resource
  • @minitech 很奇怪!我的测试结果正好相反。请参阅我的问题的附录。
  • 明确一点,你所说的“不正确的位置”实际上是正确的位置。
  • @EricLaw:嗯,它是无效的,所以浏览器真的可以决定用它做任何事情。显然,确实如此。

标签: http url uri-scheme


【解决方案1】:

不,它无效。来自RFC 3986

4.2.  Relative Reference

   A relative reference takes advantage of the hierarchical syntax
   (Section 1.2.3) to express a URI reference relative to the name space
   of another hierarchical URI.

      relative-ref  = relative-part [ "?" query ] [ "#" fragment ]

      relative-part = "//" authority path-abempty
                    / path-absolute
                    / path-noscheme
                    / path-empty

   The URI referred to by a relative reference, also known as the target
   URI, is obtained by applying the reference resolution algorithm of
   Section 5.

   A relative reference that begins with two slash characters is termed
   a network-path reference; such references are rarely used.  A
   relative reference that begins with a single slash character is
   termed an absolute-path reference.  A relative reference that does
   not begin with a slash character is termed a relative-path reference.

   A path segment that contains a colon character (e.g., "this:that")
   cannot be used as the first segment of a relative-path reference, as
   it would be mistaken for a scheme name.  Such a segment must be
   preceded by a dot-segment (e.g., "./this:that") to make a relative-
   path reference.

其中path-noscheme 专门是一条不以/ 开头的路径,其第一段不包含冒号,非常具体地解决了您的问题。

【讨论】:

  • 根据规范,这些是结构上有效的 URI,但会产生无效链接,因为相对权限(在这种情况下为域名)将被视为空(5.2.2)。由于历史原因(5.4.2),浏览器对它们的解析方式不同,但有一些一致性会很好。
猜你喜欢
  • 2013-12-29
  • 2020-07-04
  • 2011-09-06
  • 1970-01-01
  • 2017-07-05
  • 2010-11-03
  • 2019-07-17
  • 2014-10-24
  • 2023-04-06
相关资源
最近更新 更多