Links not working from subdirectories

青春壹個敷衍的年華 提交于 2020-06-27 04:01:26

问题


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)


回答1:


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>



回答2:


The home directory of a website can be accessed with a simple '/' at the start of the link you want to add. From there you can enter subfolders by appending the folder name.

Example: 'example.com/subfolder/subsubfolder/page.html'



来源:https://stackoverflow.com/questions/44029394/links-not-working-from-subdirectories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!