HTML Base Tag

This is the HTML base tag I use on my sites.

<base href="//<?php echo $_SERVER['SERVER_NAME']; ?>/">

If I move a site to another domain or test it locally, I don’t need to change every page just because the production domain is hardcoded, or vice versa. I use root-relative URLs so that links don’t depend on a hardcoded domain name.

Benefits
Consistency: Helps ensure relative URLs resolve against the intended base URL.
Portability: Makes it easier to move a site between domains or test it locally without updating every relative URL.
Simplicity: One line in the <head> can replace numerous individual URL changes.

Note: A fixed base URL is more predictable in production. A dynamic base URL, like my PHP example, can be useful across development, testing, and migration environments, provided the hostname is validated and trusted.

Why You Might Not Want to Use It

The <base> tag changes how relative URLs are resolved throughout the document. It can affect links, images, scripts, stylesheets, and in-page navigation. It may also cause problems when a site is installed in a subdirectory, when the server hostname differs from the intended public hostname, or when the hostname is not trusted. If your site already uses root-relative URLs, such as /contact.php and /images/logo.png, those URLs resolve against the current origin and don’t need the <base> tag to work across different hostnames. However, they do assume the site is installed at the domain root.

My Professional Advice:
Use the <base> tag when it solves a genuine URL-management problem. Test it in your production, staging, and local environments, and ensure any dynamically generated hostname is trusted.

Stephan Pringle

About The Author: Stephan Pringle is an Information Technology Support Specialist. He covers hardware and software and provides tips for you to troubleshoot and repair issues on your own. In his spare time, he writes articles about the State of New York on his Hackintosh and HackBook and that has helped him to become the top contributor of the New York City section of Yahoo! Answers.