0

Edit for Reopening: This is obviously not a duplicate of Absolute vs relative URLs because that page never mentions file:// URIs. This is also obvious if you just read the two code samples in this question.

I am playing around trying to make a webpage work whether it is loaded from a web server (http://) or from a local file system (file://). It seems that adjusting the <base> tag can be used to accomplish what I want, but only if I write my root-relative links like ./this instead of like /this. The <base> url will always point to the root, even when serving a file from a subdirectory. Examples:

<!-- This link resolves to "file:///C:/wwwroot/foo/bar" which is what I want -->
<html>
  <head><base href="file:///C:/wwwroot/"></head>
  <body><a href="./foo/bar">link</a></body>
</html>
<!-- But this link resolves to "file:///foo/bar" which is not what I want -->
<html>
  <head><base href="file:///C:/wwwroot/"></head>
  <body><a href="/foo/bar">link</a></body>
</html>

When I use <base href="http://localhost:1234"> both styles of link /foo/bar and ./foo/bar both resolve to http://localhost:1234/foo/bar which is what I want.

It looks like I've answered my own question: I should use ./foo/bar for root-relative links. But am I relying on undefined behavior? Will using ./foo/bar cause problems in a situation that I haven't noticed yet?

default.kramer
  • 5,943
  • 2
  • 32
  • 50
  • That's only because you aren't correctly URL-encoding the reserved characters in the local filesystem path after the `file:` scheme, namely the colon in `C:/`. Tell us what happens when you use `C%3A/` instead. – Dai Aug 05 '22 at 17:20
  • 1
    @Progman No it does not because there is nothing about using `file://` there. – default.kramer Aug 05 '22 at 17:26
  • @Dai Unfortunately no, when I use `C%3A/wwwroot/` or even `C%3A%2Fwwwroot%2F` the behavior is the same. (It would have been nice if that was the answer.) – default.kramer Aug 05 '22 at 17:27
  • 1
    @default.kramer I'll admit I'm stumped, because as per my reading of RFC 3986 it should work... (I deleted my incorrect comment that asserted `./` was root-relative: it isn't). Perhaps it's because `file:///` URIs have a lot of special/magic in them and because of their inherent security risks/problems many modern browsers just disable _all_ path-traversal tricks for `file:` URIs? (Hmm, what about UNC URIs?). Also, is there a reason you're using `file:` instead of running a lightweight local static-file webserver? – Dai Aug 05 '22 at 18:22
  • @Dai - Regarding the webserver, you're getting to the crux of my issue. If I am convinced that the `./` link style will always work, then I'd prefer not to run a webserver for simplicity's sake. But if this is undefined behavior, then I'll probably go with a webserver and the more conventional `/` link style. – default.kramer Aug 05 '22 at 18:30
  • 1
    @default.kramer Well, depending on what _exactly_ your HTML and scripts do, you might _have_ to use a local webserver in order to make use of a lot of modern web features that are only accessible from "secure" origins ( https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts/features_restricted_to_secure_contexts ) (I think some browsers, including Chrome, still treat `file:` and `http://localhost` (but not `https://localhost`) as "insecure" origins... YMMV). – Dai Aug 05 '22 at 19:10
  • @default.kramer As mentioned in the linked duplicate, when the path starts with a `/` then it is considered as the "root directory". Regardless in which "sub directory" you currently are (or what the `` element is telling you), you will get with `/` the top of the directory structure. This is the same for `file:` as for `http://`. You use `/` at the beginning of your paths when you want to build the target URL/location starting from the root directory. If you don't use `/` at the start you will have a relative path/target based on the current location (document or `` element). – Progman Aug 20 '22 at 19:58
  • @Progman Thanks, but it's still not clear to me. Could the following be a correct answer to my question? "Within the `file://` use case, if you are trying to avoid writing full paths then `./relative` links are your only choice. This is because `/root-relative` links are broken on Windows and equivalent to `//protocol-relative` links on Linux." [I just guessed on the Linux behavior.] – default.kramer Aug 21 '22 at 15:56

0 Answers0