Why do file URLs start with 3 slashes?
As others have mentioned, the file schema is in the form "file://<host>/<path>". Though most browsers won't have a problem with only two slashes, and rightfully so.
All things being equal, the triple slash and "localhost" keyword only exist to ensure conformance with valid URI/URL syntax. In the context of the file schema, the host is meaningless since it loads directly from a filesystem without any explicit transfer protocol or server document path. Because it's not HTTP, it can't load from a standard web server where in theory you could have multiple local virtual hosts set up. And it can't load from a standard network volume that's technically another "host", since the browser just uses the volume name like "file:///volumes/foo". Finally, trying things like "file://example.com/some/file" doesn't work. There's probably some reason for supporting an external host, but I can't think of any.
The IETF is currently drafting changes to remove the triple-slash requirement, though the draft also adds a few oddball possibilities like file:c|/path
and even file://///host.example.com/path
.
https://datatracker.ietf.org/doc/html/draft-ietf-appsawg-file-scheme-03
"3. This specification neither defines nor forbids a mechanism for accessing non-local files."
The complete syntax is file://host/path
.
If the host is localhost
, it can be omitted, resulting in file:///path
.
See RFC 1738 – Uniform Resource Locators (URL):
A file URL takes the form:
file://<host>/<path>
[…]
As a special case,
<host>
can be the string "localhost" or the empty string; this is interpreted as 'the machine from which the URL is being interpreted'.
Dennis has explained the 3rd slash, needed to separate the host
from the path
, but the other two are much more interesting...
It turns out they were a useless and somewhat arbitrary addition to the URL syntax. Tim Berners-Lee, inventor of the World Wide Web and author of many of its standards (including the RFC that Dennis linked to), lamented his usage of the 'double slash' in an interview back in 2009.
The double slash, though a programming convention at the time, turned out to not be really necessary, Mr. Berners-Lee explained. Look at all the paper and trees, he said, that could have been saved if people had not had to write or type out those slashes on paper over the years — not to mention the human labor and time spent typing those two keystrokes countless millions of times in browser address boxes.
http://bits.blogs.nytimes.com/2009/10/12/the-webs-inventor-regrets-one-small-thing/
So, save for a minor (and uncharacteristic) lapse in foresight some 18 years ago, your file URL could just have easily been file:/D:/Desktop/Book.pdf
, rather than file:///D:/Desktop/Book.pdf
.
There is, to answer your question, no good reason why URLs have 3 slashes.
Update: As @ComFreek points out in the comments, as of 2017, the file:/D:/...
example above is now valid! This is thanks to RFC 8089, which specifically calls out this fix from the previous standard...
According to the definition in [RFC1738], a file URL always started with the token "file://", followed by an (optionally blank) host name and a "/". The syntax given in Section 2 makes the entire authority component, including the double slashes "//", optional.
What a time to be alive.