3

I've been reading about url's. Absolute, scheme relative, root relative, location relative.
I still don't understand difference between these two:

//domain.com/index.html           - scheme relative
domain.com/index.html             - ?

.

Question 1:
Correct me if I am wrong //domain.com/index.html will resolve to absolute url like this:

http://domain.com/index.html
https://domain.com/index.html
ftp://domain.com/index.html
file://domain.com/index.html    -- if in email

And browsers will act differently: ie6 doesn't support, ie7,8 will fetch data twice(http https).

.

Question 2:
How will domain.com/index.html resolve? Same as scheme relative url in Q1? Or is it something else?

.

Question 3:
Is there any difference between these url's, what is it and why?

//www.domain.com/index.html
www.domain.com/index.html

.

Question 4:
How will //www.domain.com/index.html resolve?

.

Question 5:
How will www.domain.com/index.html resolve?

2 Answers 2

4
+50

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:

  1. If there are two leading slashes, it is scheme relative
  2. If there is one leading slash, it is root relative
  3. If there is no leading slash, but there is a colon, assume it is an absolute URI
  4. If there is no leading slash, and no colon, it is location relative
5
  • You said: "look at how many leading slashes each has: if there are none, and no colon, it is location relative" What if there is colon? ... So the answers are Q4: (scheme relative) http://www.domain.com/index.html and Q5: (location relative) http://example.com/www.domain.com/index.html provided base url is http:example.com Right?
    – CoR
    Aug 25, 2014 at 12:54
  • 1
    @CoR Yes, those would be the answers. The existence of a colon was a (possibly somewhat simplified) rule for telling an absolute URL, e.g. mailto:foo@example.com, data:text/plain,test, and urn:example would never be resolved as relative URIs. With a leading slash, however, they still would be.
    – IMSoP
    Aug 25, 2014 at 14:38
  • Finaly I got it. It was worth 50 reputation. I am accepting this answer. But please, if possible edit your answer to include : rule.
    – CoR
    Aug 26, 2014 at 9:21
  • 1
    Edited with a clearer summary and a couple of extra examples now I'm not on a mobile device. Glad it helped you. :)
    – IMSoP
    Aug 26, 2014 at 9:32
  • Honestly, you did! I thought I knew 'all' about absolute and relative links. But I could not wrap my brain about www.domain.com being interpreted as part of url path.
    – CoR
    Aug 26, 2014 at 13:24
0

They are very different. The second one is a relative reference to a path "domain.com/index.html".

WRT "domain.com" vs "www.domain.com": these are simply different host names (or path names in the second variant)

1
  • If it's not too hard, can you please explain (in common, plain, human understandable language) all 4 urls and how they resolve. I know that scheme relative will resolve like http://domain... or 'domain...` or 'domain...` What about other 3?
    – CoR
    Aug 22, 2014 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.