Arth Arth - 3 years ago 177
Apache Configuration Question

Apache ErrorDocument 404 in subdirectory without .htaccess

I'm doing some performance enhancements and came across the recommendation that

.htaccess
files can (and should) be rolled up into the
httpd.conf
.

From the Apache
.htaccess
How To
:


You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server.


As part of my initial exploration I'm just including the root
.htaccess
file in my virtual host configuration, with a view to neatening it up later.

Mostly it appears to work and in it is this directive:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpg|jpeg|png|gif|ico|swf|bmp)$ - [L,R=404,NC]


Unfortunately, we also have an
.htaccess
file in an
/images
directory with the following directive:

ErrorDocument 404 /images/notfound.gif


Which replaces any 404'd images with a 'Not Found' image.

Is this behavior possible to recreate within the root
.htaccess
? Eventually this would be included in the virtual host configuration.

I tried using the following, but wasn't surprised when it threw a 500 error:

ErrorDocument images/404 /images/notfound.gif

Answer Source

You can do this in your Virtual host .conf or the global httpd.conf (aka apache2.conf) file. The important point here is that you only do it for your /images/ directory otherwise you will get this notfound.gif in any 404 error.

e.g. for the httpd.conf or Virtualhost

<Location /images>
    ErrorDocument 404 /images/notfound.gif
</Location>

You can also use the Directory Directive

<Directory /absolute/path/to/images>
    ErrorDocument 404 /images/notfound.gif
</Directory>

If you have only one Website on your Server that you could put it in your httpd.conf but the better solution is the Virtualhost configuration.

ErrorDocument images/404 /images/notfound.gif is the wrong syntax see: https://httpd.apache.org/docs/2.4/custom-error.html

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download