4

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
  • Are all these websites using the same codebase or do all have separate web roots? You could set different 404 page for each website by just using htaccess in each sites directory. – Analog Apr 16 '16 at 16:51
  • @Analog I edit my question to show clearly what i want/need. – Julian Stark Apr 16 '16 at 17:13
  • What version of Apache are you using? – MrWhite Apr 16 '16 at 18:51
  • Server version: Apache/2.4.12 (Win32) Apache Lounge VC11 Server built: Mar 20 2015 – Julian Stark Apr 16 '16 at 18:55
5

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.

  • Thanks for this detailed answer. I updated my Apache Version an tried your proposal but I didn't work like I (You) expected. It only append the string expr=%{reqenv:SUBDIR}/error?error=404 to an blank page – Julian Stark Apr 17 '16 at 1:22
  • 1
    If I use it without the quote like {reqenv:SUBDIR}/error?error=404 it append the right Sub Folder name /website/error?error=404 but it didn't show the page – Julian Stark Apr 17 '16 at 1:35
  • Presumably you saw the literal text "/website/error?error=404"? Maybe it needs the slash prefix (/) to be present in the directive itself for it to be seen as a URL-path rather than a literal string (the "error message")? I've changed the code in my answer to reflect this. To confirm, you've found you don't need the expr=% prefix? Unfortunately I'm unable to test this myself at the current time. Whilst "Apache Expressions" are available from Apache 2.4+, being able to use them with the ErrorDocument directive is a more recent addition. – MrWhite Apr 17 '16 at 10:25
  • 1
    Now it forks fine with ErrorDocument 404 /%{reqenv:SUBDIR}/error?error=404 Thanks for help – Julian Stark Apr 17 '16 at 18:35
  • I updated your and my Code. Now it works for unlimited sub folders – Julian Stark Apr 17 '16 at 18:53
2

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: https://askubuntu.com/questions/56339/how-to-create-a-soft-or-symbolic-link

  • But in my case this isn't an option because some users (including myself) use windows. And the website should be usable without great effort like symbolic link – Julian Stark Apr 16 '16 at 22:34
  • @DestinatioN I read Apache and assumed.... well, never mind! Symlinks are simple. There is a virtual directory option on windows. I assume that this is a one time set-up thing, however, if it is not, then I assume PHP will let you create a symlink/virtual directory. If not PERL can. It is an option. If these ideas do not work for you, that is okay. I will keep thinking about it for you. Cheers!! – closetnoc Apr 16 '16 at 22:39
  • thanks for the help, but I prefer w3dk's solution – Julian Stark Apr 17 '16 at 21:31
  • @DestinatioN I don't blame you! I like it too!! w3dk is outstanding. This question is squarely within his wheel-house. He is our expert on .htaccess and regular expressions. I do these things too, but I am also completely out of practice. Oh well. That is the price of being retired. What is important is that you get the best possible answer that works for you. All else does not matter. You matter. That is why we are here. Cheers!! – closetnoc Apr 17 '16 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.