Search code examples
phphtmlhref

PHP/Html - Change link in "href" according to the file path


I created a php file where I imported my navbar which I created in a different file. The problem being that the href of the links change depending on the files and I don't see how to fix that.

For example, I have a file 1 in folder 1 where I imported my header with a link that goes to a file 2 in folder 2. If I create a file 3 with a different path, the link will no longer be valid to the file and I don't see how I can fix this.

In my navbar file :

<div class="d-none d-md-inline-block d-lg-none"> <a href="test.php"><button type="button" class="btn btn-outline-secondary mr-md-4">Se connecter</button></a> <button type="button" class="btn btn-danger mr-md-4">S'inscrire</button> </div>

In my file :

<?php include "../header/header.html"; ?>

The path to test.php will vary depending on which file I import it to.

Thanks,


Solution

  • Use a / at the beginning of the href. This will direct the link to the root (beginning) of your server path. For example, if test.html is in your parent folder, use href="/test.html".