It's very easy, looking at URLs like these, to apply your human knowledge of what they probably mean, rather than the much simpler rules implemented by software like web browsers.
The simplest type of URL (or more accurately URI, since some schemes don't represent a Location, only an Identifier) is absolute; it starts with a scheme, then a colon, and no context is needed to resolve it. Examples:
http://example.com
https://www.example.com/foo/bar.baz
http://127.0.0.1:8001
mailto:someone@example.com
data:text/plain,test
urn:example
Then there are location-relative URLs; that is, anything without a scheme, and without a leading slash. These replace everything after the slash in the current context, but leave the rest in place. If the current context is http://example.com/foo/bar.baz, you could have relative URLs like so:
bob.baz
-> http://example.com/foo/bob.baz
thing/widget.gizmo
-> http://example.com/foo/thing/widget.gizmo
example.com/page
-> http://example.com/foo/example.com/page
Note that that last example looks like a domain name at first glance, but is actually exactly the same as all the other relative URLs.
Root-relative URLs, with a leading slash, are similar, but instead of deleting after the last slash, they delete after the first. Given the same context, the previous examples become:
/bob.baz
-> http://example.com/bob.baz
/thing/widget.gizmo
-> http://example.com/thing/widget.gizmo
/example.com/page
-> http://example.com/example.com/page
A root-relative URL could also contain a colon, because the leading slash cannot be part of a scheme prefix:
/foo:bar
-> http://example.com/foo:bar
/urn:example
-> http://example.com/urn:example
Finally, there are scheme-relative URLs, with two leading slashes. They replace everything after the original double-slash, so keep only the scheme:
- if the context is
http://example.com/foo/bar
then //example.org/bob
means http://example.org/bob
- if the context is
https://example.com/foo/bar
then //example.org/bob
means https://example.org/bob
- if the context is
http://example.com
, then //foo.bar
means http://foo.bar
Note that that last example doesn't look like a domain name to us, but it still follows the same rules. Whether a URL is actually useful is not taken into account when parsing any of the relative forms.
Conventions like "begins with www." and "ends with .com" cannot be relied on, and are not used to determine if a URL is relative or not, so all you need do to answer all your questions is follow this simple set of rules:
- If there are two leading slashes, it is scheme relative
- If there is one leading slash, it is root relative
- If there is no leading slash, but there is a colon, assume it is an absolute URI
- If there is no leading slash, and no colon, it is location relative