0

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)

Matt S
  • 14,976
  • 6
  • 57
  • 76
  • 1
    If you put / at the front of a link, it starts from the domain name. So, if I am in http://example.com/some/lower/dir and I go to foo.html, it will go to http://example.com/some/lower/dir/foo.html. If, I go to /foo.html, it will go to http://example.com/foot.html. I can force a subdir by using /another/dir/foo.html. – kainaw May 17 '17 at 15:45
  • *"(a link to index.php won't work anymore if you're on the contact.php page)"* - So, where's your code? – Funk Forty Niner May 17 '17 at 15:47
  • I'd sure like to get a response from that ^ – Funk Forty Niner May 17 '17 at 15:51
  • 1
    Voted to close as unclear. I've asked for clarification but haven't seen an update to the question, nor was the comment I left responded to; the vote to close IMHO is valid. – Funk Forty Niner May 17 '17 at 15:59

2 Answers2

3

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>
Community
  • 1
  • 1
Matt S
  • 14,976
  • 6
  • 57
  • 76
  • Agreed that could work, yet I have seen questions in the past where they had to use the full system address (if local) and the OP didn't post any code or if used on a hosted service or local. If "local", then my comment should be taken into consideration. – Funk Forty Niner May 17 '17 at 15:50
  • Also, this: `Home` should work. – Sumutiu Marius May 17 '17 at 15:51
  • 2
    @SumutiuMarius No it will not. The menu needs to work from within any subdirectory. – Matt S May 17 '17 at 15:52
  • @MattS Sure. It depends of the environment. – Sumutiu Marius May 17 '17 at 15:53
  • Thanks @MattS Another and what I meant was `Home` type of thing. Then there's the server's root as per http://stackoverflow.com/a/10925538/1415724 - This thing could go either way *lol* – Funk Forty Niner May 17 '17 at 15:54
  • Oh yeah. I build my systems to avoid such things. But you're right, until we see the code it's hard to say... – Matt S May 17 '17 at 15:55
  • @MattS yeah, I asked them about it [in this comment](http://stackoverflow.com/questions/44029394/links-not-working-from-subdirectories#comment75085430_44029394) but they've yet to respond, unless they're not bothering and just want answers/solutions, or have logged off. Maybe it's lunchtime over there. – Funk Forty Niner May 17 '17 at 15:57
  • Thanks a lot, I had tried this before but it didn't work, now I found why: I'm using a localhost on my laptop as a test server and inside my htdocs I have different maps with my different websites, so it was pointing to the htdocs map instead of the "home" folder of that particular website –  May 17 '17 at 16:53
0

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'

Sam Hanson
  • 53
  • 1
  • 1
  • 7