• 08/08/2003

Custom error responses

The server can be asked to;

  1. Display some other text, instead of the NCSA hard coded messages, or
  2. redirect to a local URL, or
  3. redirect to an external URL.

Redirecting to another URL can be useful, but only if some information can be passed which can then be used to explain and/or log the error/problem more clearly.

To achieve this, Apache will define new CGI-like environment variables, e.g.

REDIRECT_HTTP_ACCEPT=*/*, image/gif, image/x-xbitmap, image/jpeg REDIRECT_HTTP_USER_AGENT=Mozilla/1.1b2 (X11; I; HP-UX A.09.05 9000/712) REDIRECT_PATH=.:/bin:/usr/local/bin:/etc REDIRECT_QUERY_STRING= REDIRECT_REMOTE_ADDR=121.345.78.123 REDIRECT_REMOTE_HOST=ooh.ahhh.com REDIRECT_SERVER_NAME=crash.bang.edu REDIRECT_SERVER_PORT=80 REDIRECT_SERVER_SOFTWARE=Apache/0.8.15 REDIRECT_URL=/cgi-bin/buggy.pl

note the REDIRECT_ prefix.

At least REDIRECT_URL and REDIRECT_QUERY_STRING will be passed to the new URL (assuming it’s a cgi-script or a cgi-include). The other variables will exist only if they existed prior to the error/problem. None of these will be set if your ErrorDocument is an external redirect (i.e., anything starting with a scheme name like http:, even if it refers to the same host as the server).

Configuration Use of “ErrorDocument” is enabled for .htaccess files when the “FileInfo” override is allowed.

Here are some examples…

ErrorDocument 500 /cgi-bin/crash-recover ErrorDocument 500 "Sorry, our script crashed. Oh dear ErrorDocument 500 http://xxx/ ErrorDocument 404 /Lame_excuses/not_found.html

ErrorDocument 401 /Subscription/how_to_subscribe.html

The syntax is,

ErrorDocument action

where the action can be,

  1. Text to be displayed. Prefix the text with a quote (“). Whatever follows the quote is displayed. Note: the (“) prefix isn’t displayed.
  2. An external URL to redirect to.
  3. A local URL to redirect to.