I am setting up my website to use the include() statement to standardize things like the header, footer, and navigation, e.g.
include('header.php);
However, when I use the same header file for pages in two different directories, the relative links contained within header.php break, e.g:
header.php
<a href="images/dog.jpg">
index.php
include('header.php);
becomes
<a href="images/dog.jpg">
This works because that is the correct path to dog.jpg from the index.php file.
animals/canine.php
include('header.php);
becomes
<a href="images/dog.jpg">
This DOES NOT work because animals/images/dog.jpg does not exist.
So, my question is, what can I change within the header.php file that will tailor the url to dog.jpg to work in both index.php and animals/canine.php when header.php is included?
Change the includes to use absolute paths:
This way they’ll be based off of the root of your site and always point to the proper folder.