: ErrorDocument in dynamic SubFolder I host different Websites in 1 web space. But I don't want to give the ErrorDocument an absolute SubFolder name. Is it possible to make website a variable?
I host different Websites in 1 web space. But I don't want to give the ErrorDocument an absolute SubFolder name.
Is it possible to make website a variable?
ErrorDocument 404 /website/error?error=404
Can .htaccess auto detect the SubFolder (including SubFolder depth)?
For example, a website written by myself where I've the same "system" twice or more e.g. productive, test and development. So all of them have different SubFolders. If someone wants to host the website by himself and use the SubFolder root/website/test/MyWebsite. So .htaccess needs to auto detect the SubFolder to redirect to the error page. So the user doesn't need to change the absolute path to website/test/MyWebsite/error....
Server version: Apache/2.4.12 (Win32)
Apache Lounge VC11 Server built: Mar 20 2015
EDIT:
This forks fine for myself
RewriteCond %{REQUEST_URI} ^/([^.]+)/
RewriteRule ^ - [E=SUBDIR:%1]
ErrorDocument 404 /%{reqenv:SUBDIR}/error?error=404
More posts by @Berumen354
2 Comments
Sorted by latest first Latest Oldest Best
Server version: Apache/2.4.12 (Win32)
Is it possible to make website a variable?
Not before Apache 2.4.13 unfortunately.
Can .htaccess auto detect the SubFolder (including SubFolder depth)?
This depends on the module you are using in .htaccess, rather than .htaccess itself. For instance, mod_rewrite (when used in a per-directory context) works relative to the directory in which it is contained and can be used to extract the subdirectory path from the URL. ErrorDocument is a core directive, paths are relative to the DocumentRoot.
If you were on Apache 2.4.13+ then... from the Apache docs for ErrorDocument:
From 2.4.13, expression syntax can be used inside the directive to produce dynamic strings and URLs.
Which means you could do "something like" this:
# Assign the subdirectory to an environment variable
# Note the slash prefix is excluded from the captured pattern
RewriteCond %{REQUEST_URI} ^/([^/]+)/
RewriteRule ^ - [E=SUBDIR:%1]
# Set ErrorDocument dynamically to this subdirectory
# Requires Apache 2.4.13+
# The 2nd argument to the ErrorDocument directive must be prefixed with a slash
# in the source code itself for it to be interpreted as a local path.
ErrorDocument 404 /%{reqenv:SUBDIR}/e404.php
Alternative - Subdomains
An alternative, that would work on all versions of Apache, is to use subdomains instead of subdirectories. The subdomain can point to the subdirectory if you wish, but you would need to access the site through the subdomain.
The important thing with the subdomain is that the document root changes. So your ErrorDocument directive can be kept simple:
ErrorDocument 404 /error?error=404
The path (starting with a slash) is relative to the DocumentRoot.
People often forget about using symlinks. You can create a symlink for each site that points to a single directory. This means that a relative URI of /error/error404.html can all point to the same directory/file even outside of the/any web root.
This answer on another SE site covers the topic enough to get started: askubuntu.com/questions/56339/how-to-create-a-soft-or-symbolic-link
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.