64

I'm loading a html file hosted on the OS X built in Apache server, within that file I am linking to another html file in the same directory as follows:

<a href="2ndFile.html"><button type="submit">Local file</button>

This works. However (for reasons too lengthy to go into) I am experimenting using the file: scheme instead, however I cannot get anything to work. Here is how I am re-writing the above line using file:

<a href="file://192.168.1.57/~User/2ndFile.html"><button type="submit">Local file</button>

(192.168.1.57 is my current IP address)

Changing it to the following does also not work:

<a href="file://Name-Of-MacBookPro/~User/2ndFile.html"><button type="submit">Local file</button>

But the file cannot be found, how should it be specified using the file: scheme?

curiousdannii
  • 1,658
  • 1
  • 25
  • 40
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
  • Unless I missed something, file:// Points to a file on the client machine... So if you are trying to get a file that is on the server, you should keep using HTTP... – Salketer Oct 03 '12 at 15:27

5 Answers5

86

The file: URL scheme refers to a file on the client machine. There is no hostname in the file: scheme; you just provide the path of the file. So, the file on your local machine would be file:///~User/2ndFile.html. Notice the three slashes; the hostname part of the URL is empty, so the slash at the beginning of the path immediately follows the double slash at the beginning of the URL. You will also need to expand the user's path; ~ does no expand in a file: URL. So you would need file:///home/User/2ndFile.html (on most Unixes), file:///Users/User/2ndFile.html (on Mac OS X), or file:///C:/Users/User/2ndFile.html (on Windows).

Many browsers, for security reasons, do not allow linking from a file that is loaded from a server to a local file. So, you may not be able to do this from a page loaded via HTTP; you may only be able to link to file: URLs from other local pages.

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
  • 1
    how to use **relative path** instead of C:// on Windows? – Lei Yang Mar 23 '17 at 07:30
  • To use a relative path, you just include the relative path, without any `file:` scheme or `//`. So if you have a file `index.html`, and want to refer to `other_file.html` in the same directory, you would just link directly to `other_file.html` with no scheme. – Brian Campbell Mar 23 '17 at 16:45
  • @Brian then there is no any way to download local file like file:///home/User/2ndFile.html ? I would like to know if there is any alternative available? – Rrptm Feb 14 '21 at 19:05
4

The 'file' protocol is not a network protocol. Therefore file://192.168.1.57/~User/2ndFile.html simply does not make much sense.

Question is how you load the first file. Is that really done using a web server? Does not really sound like. If it is, then why not use the same protocol, most likely http? You cannot expect to simply switch the protocol and use two different protocols the same way...

I suspect the first file is not really loaded using an apache http server at all, but simply by opening the file? href="2ndFile.html" simply works because it uses a "relative url". This makes the browser use the same protocol and path as where he got the first (current) file from.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • The first file is loaded by a client application, the reason I'm using file:// is I'm experimenting with some security stuff and seeing if its possible for the first file to use file://. – Gruntcakes Oct 03 '12 at 15:35
  • Well that's all fine (the little you tell). But if you use the file protocol this has _nothing_ to do with apache. Apache is a http server. – arkascha Oct 03 '12 at 15:52
  • I realize that when this answer was written it may have not been the case: The `file` _scheme_ is one of the schemes mentioned in the [RFC1738](https://datatracker.ietf.org/doc/html/rfc1738/) and defined in [RFC8089](https://datatracker.ietf.org/doc/html/rfc8089). The [URL Living Standard](https://url.spec.whatwg.org/) has examples using the `file` scheme. – Orestis Kapar Aug 12 '22 at 15:23
  • @OrestisKapar It was the case, but none of that is relevant. – Jim Balter Aug 31 '23 at 21:09
4

the "file://" url protocol can only be used to locate files in the file system of the local machine. since this html code is interpreted by a browser, the "local machine" is the machine that is running the browser.

if you are getting file not found errors, i suspect it is because the file is not found. however, it could also be a security limitation of the browser. some browsers will not let you reference a filesystem file from a non-filesystem html page. you could try using the file path from the command line on the machine running the browser to confirm that this is a browser limitation and not a legitimate missing file.

james turner
  • 2,773
  • 1
  • 21
  • 24
1

I had similar issue before and in my case the file was in another machine so i have mapped network drive z to the folder location where my file is then i created a context in tomcat so in my web project i could access the HTML file via context

0

For apache look up SymLink or you can solve via the OS with Symbolic Links or on linux set up a library link/etc

My answer is one method specifically to windows 10.

So my method involves mapping a network drive to U:/ (e.g. I use G:/ for Google Drive)

open cmd and type hostname (example result: LAPTOP-G666P000, you could use your ip instead, but using a static hostname for identifying yourself makes more sense if your network stops)

Press Windows_key + E > right click 'This PC' > press N (It's Map Network drive, NOT add a network location)

If you are right clicking the shortcut on the desktop you need to press N then enter

Fill out U: or G: or Z: or whatever you want Example Address: \\LAPTOP-G666P000\c$\Users\username\

Then you can use <a href="file:///u:/2ndFile.html"><button type="submit">Local file</button> like in your question


related: You can also use this method for FTPs, and setup multiple drives for different relative paths on that same network.

related2: I have used http://localhost/c$ etc before on some WAMP/apache servers too before, you can use .htaccess for control/security but I recommend to not do so on a live/production machine -- or any other symlink documentroot example you can google

skaviouz
  • 45
  • 7