How to enable custom error messages on the Apache web server
This document describes how to configure the Apache web server so that it will redirect common server errors - like 404 Not Found or 500 Internal Server Error - to a Perl CGI script for processing.
In order to configure Apache error handling, you must:
Edit a ".htaccess" file in the root folder of your web site
Have the "AllowOverride" privilege granted by the system administrator who set up Apache. (This is automatically the case for the majority of Apache users.)
If you do not have the AllowOverride privilege, then you will have to ask your administrator to take these steps on your behalf.
Follow these steps to add a custom error page to Apache:
Before you tweak Apache, first make an initial request to your custom error page using its direct URL. Make sure it displays properly. If your error page is a Perl CGI script, and it is failing, then it will be very difficult to debug what is going on when other errors are redirected to it.
Some FTP servers and FTP client software do not allow the viewing or manipulation of files that begin with a dot, like ".htaccess". Some systems will allow you to upload files named ".htaccess", but once the file is on the server, the file won't appear in your file list and you won't be able to delete or rename it - which will be a problem if the .htaccess file makes your site inaccessible.
So, first, create an empty file named ".foo". Upload it to your site. Will your FTP client software allow it? Will the FTP server accept it? Can you then rename and delete the file? If you have trouble with any of these operations, then you should not proceed to .htaccess. Instead contact your system administrator to see about getting the right software/settings for the job.
Before beginning, request a file from your website that doesn't exist, like www.foo.com/no_such_file. Observe the 404 Not Found error handling: do you see a plaintext file like "404 Not Found"? Or do you see a pretty error message that someone else has already customized, perhaps one specific to your web hosting company? Or are you redirected to a special file, like www.foo.com/404.html? You need to know what the baseline behavior is so that you can evaluate the behavior later after you start tweaking things.
Edit the file named ".htaccess" in the root folder of your web site. Create the file if necessary. Add the following lines, where the path "/cgi-bin/guardian/ag.pl" has been replaced with your path to your Perl script:
ErrorDocument 401 /cgi-bin/guardian/ag.pl ErrorDocument 403 /cgi-bin/guardian/ag.pl ErrorDocument 404 /cgi-bin/guardian/ag.pl ErrorDocument 500 /cgi-bin/guardian/ag.pl
You may want to review this copy of a valid .htaccess file.
Upload the updated .htaccess file. Be sure to upload it in ASCII mode. Be sure to apply read permissions to the file (chmod 744).
Immediately after uploading, make a test request for any valid file on your site. If Apache has been configured such that it doesn't allow overrides of the ErrorDocument directives, or if you are running an older version of Apache that doesn't understand ErrorDocument, or if there is some error with your file, then all requests to your web site will fail!! You want to check whether there are problems with normal requests.
If all normal requests fail after you updated the .htaccess file, immediately delete or rename it. If you had an obvious error, like misspelling ErrorDocument, then try to correct it and upload again. Otherwise check with your system administrators about whether your web site allows for custom error handling.
So, now the .htaccess file has been updated and uploaded, and normal requests work okay. Make a test request for a file that doesn't exist. One of four things will usually happen:
You will be redirected to your custom error page. Yay!
You will see the standard Apache error message, or the behavior will be the same as whatever it was before you updated the .htaccess file (which you checked in step 3). If this happens, then you probably don't have the AllowOverride privilege, and you will have to bother your administrator.
You will see something like this: "Not Found; The requested URL /scripts/guardian/test/a was not found on this server; Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request". This means that you need to review the path the follows "ErrorDocument 404" in your .htaccess file.
You will see something similar, like this: "Not Found; The requested URL /scripts/guardian/test/a was not found on this server. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request". This means that the Perl CGI script you have set up to handle errors has failed. You should go back and debug that script further. If you are using Guardian, use the "System Test" option from the Admin Page to exercise the error-handling code paths.
You may want to review the Apache HTTP Server: Custom error responses help article.
"How to enable custom error messages on the Apache web server" https://www.xav.com/scripts/guardian/help/1001.html