Base href, the simple solution

Posted: October 13th, 2014 | Author: | Filed under: HTML, Internet, PHP | No Comments »

I’ve been grappling for a while with a problem. The site I’d created, due to using mod_rewrite to create URLs that didn’t actually exist, was creating a virtual folder structure that meant that I was having to create specific sidebars and navigation elements that included the right level of linking.

This being a PHP site, I’d tried to resolve this by using variables such as $_SERVER[‘PHP_SELF’] to try and automatically resolve the server address to provide a relative file path, but this had mixed results.

Turns out that the answer was the not frequently mentioned base html tag – this is a tag that I was sort of aware of, but had forgotten. Indeed, an otherwise excellent HTML book I read the other day didn’t even mention it…

Simply adding

 <base href = "http://thesiteinquestion.com" /> 

to the site made included headers, sidebars etc instantly portable, no matter the folder depth.

Of course, this being a PHP site, I set the base URL in my site’s config file i.e.

$baseurl = "http://thesiteinquestion.com";

then called that in my site header:

 <base href = "<?php echo $baseurl ?>" /> 

so I only have to change the config file for different servers/environments.



Leave a Reply