简体   繁体   中英

how to link to same folder and different folder with html href using header.php template?

if i am at the current address: "pages/tasks.php"
then click a link that has:
<li><a href="./pages/task-form.php">Post Task</a></li> my address becomes:
"/pages/pages/task-form.php"
i understand its because it is opening that link on top of my current folder depth. I know I can put all of the pages in the same folder, then delete the "./pages/". If I do that, then I cant access another page in a different folder. But what if I want to be able to change directories with pages, or stay in the same folder at the same time? I am using a header.php template for all pages.

If you are using MySQL, it will look something like this: localhost/folder/pages/task-form.php Simply use as many '../' as necessary to back out directories until you get to the destination that contains the main folder. in my example mentioned above, the file is 2 folders deep, so it requires 2 "../"
For this use:
<li><a href="../../folder/pages/task-form.php">Post Task</a></li>
For your example:
<li><a href="../pages/task-form.php">Post Task</a></li>

This will always take you to the main folder, then go to the destination of the file, even if youre already at it.
The way you were doing it was simply opening the destination folder, and the file again, while still being at the folder, thus creating a duplicate of the folder.
Important: You must use as many "../" as the deepest file you are accessing if it has a header.php linked to it. You can use 10 of "../" even if youre not 10 deep. It cant back out more than the most containing folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM