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?