Good day to all.

Site header , navigation and basement are in separate php files inside the pages folder ( mysite.ru/pages/header.php , etc.). The navigation file contains relative links to the pages. If I use index.php (it is located in the root of the site, with the help of include "file_name.php"; include the necessary files), then everything works correctly. If I refer to these files from a subfolder, then the links no longer go to search for files in the root, but in this very subfolder (for example: mysite.ru/folder/index.php instead of mysite.ru/index.php ). How to fix this moment in navigation?

PS If my solution (only index.php is in the root, header, navigation and basement in the pages folder, and files on sections of the site in other folders) is stupid / incorrect, then tell me how best to do it.

  • Use DOCUMENT_ROOT to base file paths - vp_arth
  • @vp_arth, does not come out. refers to some rubbish. Maybe on the physical location of the file, damn it knows. This is the name of the site displays: /var/www/domains/t/te/tes/ - Ivan Lazarev
  • Well, of course relative links are not an option at all. Personally, I just set the path constant of the root, ala define('ROOT_PATH','app/files/') and dance from it. - SLy_huh

3 answers 3

Inklyadam this issue has nothing to do.

The navigation file contains relative links to the pages.

It is not right. Links must be absolute :

in order for any link to work, no matter where it is called from, it must be absolute .

If you have, say, two sections on your site:
http://www.example.com/about/info.php
and
http://www.example.com/job/vacancy.php

then, if you simply link to vacancy.php in the info.php file, the browser will not find it - it will search for the address http://www.example.com/about/vacancy.php , completing the path from the current directory.

Therefore it is necessary to write the full path from the root of the site - /job/vacancy.php
All this concerns, naturally, not only tags but also any other, where references to other files are used.

Links to local addresses should be written without specifying the protocol and domain - only the path from the site root is /job/vacancy.php. Links to other sites should be written in full - http://www.example.com/job/vacancy.php .

  • Please make a brief excerpt from the article to which you refer. One day it will disappear, and the answer will remain. - vp_arth
  • @Ipatiev, this is all interesting, but when I try to find out from the $_SERVER its 'SERVER_NAME' I, to put it mildly, still don’t give a link to the site’s root. Maybe I'm doing something wrong. Already do not even know. - Ivan Lazarev
  • NO YOU NEED ANY 'SERVER_NAME'! Here it is written in Russian in yellow. References to local addresses should be written without specifying the protocol and domain - only the path from the root of the site is /job/vacancy.php. Link to the root of the site does not need to "learn". It is always the same - "/". From it must begin all the links on the site - Ipatiev

to return to the level above write ../ . If you need to access the folder in the next folder, then first exit from the current folder and go to the desired one:

../folder/file.php

Perhaps you would rather use the absolute path ..

"http://" . $_SERVER['SERVER_NAME'] "http://" . $_SERVER['SERVER_NAME'] .. and write the rest of the path in the links. Nd:

"http://" . $_SERVER['SERVER_NAME'] . "/folder/header.php" "http://" . $_SERVER['SERVER_NAME'] . "/folder/header.php" .

Then no matter where you are accessing the file, the link does not change - there is no confusion

  • "/var/www/domains/t/te/tes/" - first writes this, and only then the name of the site and the link to the page. Of course, the 404th immediately falls. - Ivan Lazarev
  • Corrected .. used the wrong variable .. it was to access files inside the code - Volodymyr
  • No need to try to correct a knowingly response curve. If there is no knowledge, then no corrections will help - Ipatiev

It is better to use the MVC structure, it may not be as convenient as in your case, but it is more correct and there will be no confusion.

  • one
    I guess you mean Front Controller - vp_arth
  • @vp_arth as option =) - Yevgeny Ignatyev
  • And if he has a 2 page landing page without backend logic? It looks more like a comment than an answer. - SLy_huh
  • @SLy_huh He asked for advice in his question, and if he has a landing page, then why complicate things like that? What prevents to display the menu immediately in the text of the page, I do not think that on the landing page it should somehow be dynamically generated. - Yevgeny Ignatyev
  • one
    Well, for one-line advice, it’s more logical to use комментарии . Your answer does not correlate in any way with the question itself. Splitting a page into blocks does not always imply MVC. - SLy_huh