Search code examples
phphtmlhome-directory

Links not working from subdirectories


I want to make multiple pages on my website, but to keep everything clean I want to make different directoriess with the different pages. However, I use php to make a different file with my header that is included in all my pages, so I only have to change the code of my header once and it will be the same on all pages.

The problem is that the links I use in my menu items (like home, contact, about, etc.) will not work anymore when you're on a page inside a directory (I'll make an example below).

So my question: Is there a home folder on a website (like ~/ on unix) or is there another way to make it work?

Example of my directory structure:

htdocs
    index.php
    header.php
    menus
        contact.php
        about.php

(a link to index.php won't work anymore if you're on the contact.php page)


Solution

  • Sounds like you're using relative paths in your menu links. Use an absolute path instead by starting with a "/":

    <a href="/index.php">Home</a>
    <a href="/menus/about.php">About</a>
    

    or a complete URL:

    <a href="http://example.com/index.php">Home</a>
    <a href="http://example.com/menus/about.php">About</a>