180

HTTP starts with two slashes. E.g. http://example.com.

Same goes for FTP. E.g. ftp://example.com.

However, file "URLs" start with three slashes. E.g. Reading a pdf file using chrome, the URL would be file:///D:/Desktop/Book.pdf.

Why do file URLs use three slashes?

14

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://tools.ietf.org/html/draft-ietf-appsawg-file-scheme-03

"3. This specification neither defines nor forbids a mechanism for accessing non-local files."

  • 1
    The draft has become RFC 8089 in 2017, which still includes your quotation. – ComFreek Mar 7 at 10:46
248

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'.

  • 3
    Cool, I'd didn't expect the answer to this question a RFC standard! – Pacerier Oct 30 '11 at 14:47
  • 33
    @Pacerier Almost anything that has to do with the internet can be explained by an RFC (note that they're not necessarily "standards" but may be adopted as such). – slhck Oct 30 '11 at 14:53
  • 4
    Note that Tim Berners Lee has apologized for those 2 slashes that are in every URL: news.bbc.co.uk/2/hi/technology/8306631.stm – Peter Oct 31 '11 at 19:56
  • 7
    Can I omit the localhost from other protocols too or does it work only for file://? – Agos Nov 1 '11 at 11:34
  • 3
    Note that Firefox doesn't really follow this standard ` file://test/C:\ ` will behave the same as ` file:///C:\ ` and ` http:///test ` will give an invalid URL error – Earlz Nov 1 '11 at 15:02
26

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.

  • 2
    TimBL also elaborates on this in his FAQ – Molomby Sep 25 '12 at 8:32
  • 2
    Not to mention that 2 bytes could be saved by just using http:example.com instead of http://example.com It may not seem like a lot, but they add up. Google receives millions of searches a day. How many links are on a page? At least 20. That means for a million searches, if the slashes weren't needed, 20 MB of bandwidth could have been saved. – Cole Johnson May 25 '14 at 0:23
  • 1
    @ColeJohnson - Did you know you can leave out the protocol part too? So http://example.com could be linked to as //example.com in a document transmitted over http. It's called a protocol relative url, all browsers support them. – Molomby Jun 13 '14 at 3:08
  • I am well aware of those, but I personally only use them in CSS. When writing HTML, I use the protocol also. No real reason really. Except maybe because when HTML5+CSS3 first became "big" a few years ago, almost all the sites I looked at were like that. – Cole Johnson Jun 13 '14 at 4:18
  • 1
    Contrary to what your answer might suggest, file:/D:/Desktop/Book.pdf is a valid file URI as per RFC 8089 (from 2017), which superseded RFC 1738 (1994) in file URI aspects. – ComFreek Mar 7 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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