Whenever I want to include a document with PHP, or perform any other PHP action which requires a path to be described, I need to write something like, ../../../../../document.html. This works, but it’s tedious, and in some cases, the path is wrong, resulting in code appearing on-page, and debugging.
This can, obviously, be bypassed by using the $SERVER_['DOCUMENT_ROOT'] command, but that, too, requires a sometimes unmanageable amount of code (again, when many, many paths are present).
Is there any way to simply define all PHP paths site-wide to be document root-relative, as in HTML (/document.html is root relative)?
Whenever I want to include a document with PHP, or perform any other PHP
Share
I used to do that and have those problems. Then I switched my site to use
mod_rewritefor the urls. I then had all of my php pages in the same directory so I didn’t have to go a confusing 4 times up the directory structure to find the root. You can have a php file on your server at:And, using mod_rewrite in your .htaccess file, you can have that map to:
When I moved over to that structure, it really helped me on the php side of things specifically regarding navigating to different directories.