Are protocol-relative URLs relative URLs?

醉酒当歌 提交于 2019-12-01 03:32:35

Every relative URL is an unambiguous URL given the URL it is relative to. So if your page is http://mypage.com/some/folder/ then you know the relative URL this/that corresponds to http://mypage.com/some/folder/this/that and you know the relative URL //otherpage.com/ resolves to http://otherpage.com/. Importantly, it cannot be resolved without knowing the page URL it is relative to.

A relative URL is any URL that is relative to something and cannot be resolved by itself. An aboslute URL does not require any context whatsoever to resolve.

What you are calling a “protocol-relative URL” WHATWG calls a “scheme-relative URL” in the URL Standard document, and it is not an absolute URL, but a relative URL.

Granted most sites available on HTTPS show the same content on the corresponding HTTP URLs, that is not necessarily the case, and it therefore makes sense a URL that does not include the scheme cannot be considered absolute.

From the document:

An absolute URL must be a scheme, followed by ":", followed by either a scheme-relative URL, if scheme is a relative scheme, or scheme data otherwise, optionally followed by "?" and a query.

Specifically answering your question, we have:

A relative URL must be either a scheme-relative URL, an absolute-path-relative URL, or a path-relative URL that does not start with a scheme and ":", optionally followed by a "?" and a query.

At the point where a relative URL is parsed, a base URL must be in scope.

Examples (brackets indicate optional)

path-relative URL [path segment][/[path segment]]…

  • about
  • about/staff.html
  • about/staff.html?
  • about/staff.html?parameters

absolute-path-relative URL: /[path-relative URL]

  • /
  • /about
  • /about/staff.html
  • /about/staff.html?
  • /about/staff.html?parameters

scheme-relative URL: //[userinfo@]host[:port][absolute-path-relative URL]

  • //username:password@example.com:8888
  • //username@example.com
  • //example.com
  • //example.com/
  • //example.com/about
  • //example.com/about/staff.html
  • //example.com/about/staff.html?
  • //example.com/about/staff.html?parameters

absolute URL: scheme:[scheme-relative URL][?parameters]

  • https://username:password@example.com:8888
  • https://username@example.com
  • https://example.com
  • https://example.com/
  • https://example.com/about
  • https://example.com/about/staff.html
  • https://example.com/about/staff.html?
  • https://example.com/about/staff.html?parameters

relative URL:

  • Anything from scheme-relative URL list
  • Anything from absolute-path-relative URL list
  • Anything from path-relative URL list

Note: This answer does not disagree with the first answer, but it was only somewhat clear to me that post answered the question after reading it several times and doing further research. Hopefully this answer spells it out better for others stumbling on this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!