ipel
ipel

Reputation: 1348

htaccess custom directory for error 404

I have a website where each of my users have a cms installed in a different directory, the structure is like:

/ (root)
/cms_user1/
/cms_user1/404.php
/cms_user1/.htaccess
/cms_user2/
/cms_user2/404.php
/cms_user2/.htaccess

In each .htaccess file there is:

ErrorDocument 404 /404.php

The problem is that in this way the page 404.php isn't loaded on /cms_client1/404.php but instead in the root, that is /404.php

There is a way to load the 404.php in the user directory automatically, without specify the name of the directory?

PS: the only way i found till now is to specify the directory name in each .htaccess (that in my case is not the best option because i have to edit each htaccess), like this:

ErrorDocument 404 /directory/404.php

Upvotes: 2

Views: 1475

Answers (1)

anubhava
anubhava

Reputation: 785108

Bad News: Using ErrorDocument you cannot do it.
Good News: It is possible using mod_rewrite rules.

In each of the .htaccess shown in your question you can have a rule like this:

RewriteEngine On

# Determine the RewriteBase automatically/dynamically
RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=BASE:%2]

# if request is not for a file
RewriteCond %{REQUEST_FILENAME} !-d
# if request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-f
# forward it to 404.php in current directory
RewriteRule . %{ENV:BASE}/404.php [L]

Upvotes: 5

Related Questions