Are protocol-relative URLs relative URLs?
Asked Answered
P

2

12

So consider a protocol-relative URL like so;

//www.example.com/file.jpg

The idea I've had in my head for as long as I can remember is that protocol-relative URLs are in fact absolute URLs. They behave exactly like absolute URLs, and never do they work like relative URLs. I wouldn't expect this to make the browser go find something at

http://www.example.com///www.example.com/file.jpg

The URL defines the host and the path (like an absolute URL does), and the scheme is inherited from whatever the page used, and therefore it makes a complete unambiguous URL, i.e. an absolute URL.

Right?

Now, upon further research into this, I came upon this answer, which states;

A URL is called an absolute URL if it begins with the scheme and scheme specific part (here // after http:). Anything else is a relative URL.

Neither the question nor the answer specifically discuss protocol-relative URLs, so I'm mindful that it can just be an oversight in wording.

However, I'm now also now running into an issue in my development, where a system that only accepts absolute URLs doesn't function with protocol-relative URLs, and I don't know if that's by design or due to a bug.

The RFC3986 section which is often linked to in relation to protocol-relative URLs also splashes the word "relative" around a lot. 4.3 then goes on to say that absolute URIs define a scheme.

All this evidence against my initial assumption led me to the question;

Are protocol-relative URLs relative or absolute?

Pious answered 22/3, 2013 at 23:28 Comment(0)
P
5

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.

Picky answered 22/3, 2013 at 23:32 Comment(0)
F
3

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:[email protected]:8888
  • //[email protected]
  • //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:[email protected]:8888
  • https://[email protected]
  • 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.

Filip answered 20/11, 2014 at 7:35 Comment(1)
"absolute-path-relative URL" is not (or is no longer) in the URL spec...Myrtamyrtaceous

© 2022 - 2024 — McMap. All rights reserved.