Why do file URLs start with 3 slashes?

183

39

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?

Pacerier

Posted 2011-10-30T14:25:08.813

Reputation: 22 232

Also see http://stackoverflow.com/q/22772897/632951

– Pacerier – 2015-01-23T09:04:14.307

5Opera for Windows expands it to file://localhost/D:/Desktop/ automatically. – None – 2011-12-27T21:11:02.067

Answers

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

Beejor

Posted 2011-10-30T14:25:08.813

Reputation: 266

1

The draft has become RFC 8089 in 2017, which still includes your quotation.

– ComFreek – 2019-03-07T10:46:42.877

254

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

Posted 2011-10-30T14:25:08.813

Reputation: 42 934

Is it actually possible to use hostnames other than localhost? Is it equivalent to accessing another host on the local network? In what situation has people ever used hosts other than localhost? – CMCDragonkai – 2015-08-31T06:43:47.730

What about the file( /Users/john/filename.crt ) located on local mac OS, would it be file:///Users/john/filename.crt ?? – Learner – 2018-02-14T19:27:43.993

The cited RFC 1738 (from 1994!) has been superseded by RFC 3986 (from 2005). The latter only marginally mentions file URIs, which have now been outsourced to RFC 8089 (2017). Especially, file:/path/to/file is also a valid file URI (as claimed in appendix A).

– ComFreek – 2019-03-07T10:45:11.390

3Cool, I'd didn't expect the answer to this question a RFC standard! – Pacerier – 2011-10-30T14:47:14.293

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 – 2011-10-30T14:53:18.870

2

@slhck thx for the info =) Btw I was wondering about this for quite some time but what's with the image @ http://qweop.com/x3 ?

– Pacerier – 2011-10-30T15:43:47.733

2Perhaps someone should write a blog post about this, explaining how this stuff works in more detail? – Ivo Flipse – 2011-10-31T06:17:44.743

1That's quite a popular answer for someone only 15 days into their SU career. Well done Dennis and welcome aboard. – music2myear – 2011-10-31T19:14:34.097

5

Note that Tim Berners Lee has apologized for those 2 slashes that are in every URL: http://news.bbc.co.uk/2/hi/technology/8306631.stm

– Peter – 2011-10-31T19:56:40.190

7Can I omit the localhost from other protocols too or does it work only for file://? – Agos – 2011-11-01T11:34:51.717

3Note 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 – 2011-11-01T15:02:56.390

Gotta love the RFCs. I don't see why TBL needs to apologize for two slashes. I think they are pretty cool. – Cris Stringfellow – 2013-02-26T22:27:57.550

29

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.

Molomby

Posted 2011-10-30T14:25:08.813

Reputation: 1 486

@Molomby, He is talking about all the bytes wasted when people don't do relative protocol urls. Which is like >99% of population. – Pacerier – 2015-01-23T08:56:33.527

@ColeJohnson, So you are recommending h:example.com instead of http:example.com? – Pacerier – 2015-01-23T08:57:05.647

@Pacerier while that would save even more bytes, I wouldn't have recommended that. A single h is not clear on what it means unlike http (which is clear). Also, take ftp and file for example. If we use your idea of using the first letter of the acronym only, they'd both be f. Sure, the stuff after the protocol would remove the ambiguity (is it a file path of web path), but that would be needlessly complex – Cole Johnson – 2015-01-23T18:42:47.143

2

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 – 2019-03-07T11:03:17.333

2

TimBL also elaborates on this in his FAQ

– Molomby – 2012-09-25T08:32:51.267

2Not 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 – 2014-05-25T00:23:55.953

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 – 2014-06-13T03:08:20.557

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 – 2014-06-13T04:18:20.310