Explain Codes LogoExplain Codes Logo

Having links relative to root?

web-development
responsive-design
best-practices
Nikita BarsukovbyNikita Barsukov·Oct 20, 2024
TLDR

To ensure absolute paths on your site, start links with /. This symbol stands for the root of your domain:

<!-- Look ma, no hands: I am anchoring right to the root! --> <a href="/your/path/here.html">Link</a>

This root-relative link will reliably point to example.com/your/path/here.html, regardless of the current location on your site, preventing broken links when you decide to go about moving files.

You can also use a <base> tag in your document <head> to set a common base path for all links:

<head> <!-- Hey look, I am your base now! All your links belong to me! --> <base href="/"> </head>

Now, all relative URLs will resolve from the root URL you specified in the base tag.

Pros of root-relative URLs

Choosing root-relative links versus their relative counterparts comes with a few compelling gains:

  • Efficiency: No more updating paths when a movable feast of files gets shuffled around your site.
  • Clarity: By starting from the root, you bypass the pain of navigating perplexing nested directories.
  • Simplicity: Yield less verbose code when referencing site-wide assets, like stylesheets or scripts.

Understanding <base> tag dosage

Just as with many things in life, the <base> tag is best applied in moderation:

  • Set once per page: Mold the base URL at the dawn of your document.
  • Overrides relative paths: Embedded paths that dare to differ from the <base> will be overridden.
  • SEO implications: Beware of unwelcome surprises like duplicate content when misused.

Root-relative URL drawbacks

Remember, every silver lining has a cloud. Here are a few potential shocks in store with root-relative URLs:

  • No freedom beyond the frontier: You can't use root-relative URLs for reaching distant external domains.
  • Machinery mechanics: Azure, AWS, Google Cloud... your server setup must be primed to correctly map the root directory.
  • Beware of subdirectories: Hosting in a subdirectory like example.com/blog? Anchoring to the root might leave you stranded.

The ABC of path resolution

Here's a quick rundown of the way different path notations govern your website:

  • Relative paths (../): Travelling higher up the directory tree, like climbing a flight of stairs.
  • Current directory (./): Looking for connections within your vicinity, like grabbing a file from your room.
  • Root directory (/): A homing beacon hauling you back to the root; think of going home after a long day.

Path tales across web frameworks

How you tread root-relative paths may differ depending on your chosen web framework:

  • In ASP.NET, <base href="~/" /> is your magical incantation to seeing paths resolved correctly.
  • PHP treads the PHP way: Huddle close to the $_SERVER['DOCUMENT_ROOT'] variable to grab the dynamic root location.
  • JavaScript Single Page Apps (SPAs): Pets like React or Angular may require a callback to context-specific methods to handle root-relative paths.

Invest some time into a robust testing setup:

  • Unit tests: Make these your best friend when checking the soundness of your URLs.
  • Link checkers: The humble but effective W3C Link Checker can cover your site like a hawk, catching those elusive bad links.
  • Continuous integration: A stitch in time saves nine - link verification as part of your deployment pipeline can catch snafus early.

Going with relative URLs

There are instances where relative URLs might carry the day:

  • Shape shifting: Having a mobile app and a website presents different environments. Relative links adapt better to both.
  • Anticipation: When the final domain is pending, relative URLs offer a bridge to transition.
  • Changing of the guards: Weather a shift in domain name or server environment lightly with relative paths – they're unfazed by the domain they belong.