I'm writing a CMS in php, so I'd like to allow users to change the name of the root folder without breaking the application. I'm currently trying to set up some file handling for custom error pages using my directory-specific .htaccess file. My file tree is relatively as follows:
MAMP >
| htdocs >
| | cms >
| | | .htaccess
| | | index.php
| | | cms_files >
| | | | info.json
| | | | . . .
| | | | error_pages >
| | | | | errors.php
| | | | |
Whereas the folder I've labled 'cms' should be able to be modified.
So my issue is that in my .htaccess file I'm using ErrorDocument 404 /cms/cms_files/error_pages/errors.php
, handling the error codes in that document, which works just fine. But, like I said, I need to be able to change the name of 'cms' without breaking the ErrorDocument file path, and from all of my searching online I cant seem to find anything that will allow me to set up a relative path in .htaccess, relative to the directory where it is currently located.
Essentially I would like to be able to do something like
ErrorDocument 404 CURRENT_DIRECTORY/cms_files/error_pages/errors.php
to avoid hardcoding the root folder.
Is there any easy method of doing this? or maybe an alternate option using just php instead of .htaccess? Thanks in advance!
p.s. The .htaccess file and error_pages folder must remain in their respective directories in case users would like to create multiple installations of the cms, all installation-specific information must be contained in the one (in this case the 'cms') folder. Which is another caveat as to why the folder must be able to change name without breaking.
According to Apache documentation, it looks like relative paths simply aren't supported: (https://httpd.apache.org/docs/2.4/custom-error.html)
The syntax of the ErrorDocument directive is:
ErrorDocument <3-digit-code> <action>
where the action will be treated as:
A local URL to redirect to (if the action begins with a "/").
An external URL to redirect to (if the action is a valid URL).
Text to be displayed (if none of the above). The text must be wrapped in quotes (") if it consists of more than one word.
However, if you read further down in the documentation, it sounds like you can pass additional data to the redirected URL:
Redirecting to another URL can be useful, but only if some information can be passed which can then be used to explain or log the error condition more clearly.
To achieve this, when the error redirect is sent, additional environment variables will be set, which will be generated from the headers provided to the original request by prepending 'REDIRECT_' onto the original header name. This provides the error document the context of the original request.
Read the documentation for all of the redirect information available. You might find something useful there.
Perhaps you redirect all 404s to some sort of controller that redirects the user to the correct 404 page via PHP, given the additional redirect information.