Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Root-relative links in HTML sites that work both locally and on the web

Root-relative links are usually accomplished by prefacing a href attribute with /, like so:

<a href="/path_to_file">

The problem is that this doesn't work with locally run sites, as the / will link to actual root of the computer's filesystem rather than the website's base URL.

Is there a way to make root-relative links that function properly not only in traditional web-hosted cases but also when run locally?

like image 830
lochnesscookie Avatar asked Oct 16 '25 19:10

lochnesscookie


1 Answers

You can provide a base URI in the head:

<base href="https://example.com/" />

With this base element, <a href="/path_to_file"> will link to https://example.com/path_to_file, no matter the current document host.

like image 143
unor Avatar answered Oct 18 '25 09:10

unor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!