You are here: Home > Blog > Topics > apache

apache

Aug 17, 2009

ErrorDocument and mod_rewrite workaround

by Bill Gannon — last modified Aug 17, 2009 12:40 PM
Filed Under:

The standard error messages that come with Apache can be very confusing for clients and visitors alike. Our most common error message, caused whenever a zope instance is taken down or restarted is 'Bad Gateway', which is about as user-unfriendly as an error message can get.

Apache comes with a directive to allow administrators to customise the error message for any error, as specified by its number. For example, the error number for a Bad Gateway is 502, therefore the directive:

ErrorDocument 502 /error502.html

should change the Bad Gateway message to whatever is specified in the file error502.html.

All would be well, you would think, except for the fact that all of our Plone hosting uses mod_rewrite, which would produce an apache vhost config such as this:

<VirtualHost 89.167.224.29:80>
  ServerAdmin hostmaster@openia.com
  ServerName yourdomain.co.uk
  RewriteEngine on
  RewriteRule ^/(.*) http://192.168.131.85:13080/VirtualHostBase/http/%{SERVER_NAME}:80/plone/VirtualHostRoot/$1 [P]
  DocumentRoot /var/www
  ErrorDocument 502 /error502.html
</VirtualHost>

Unfortunately if you try this out you will (rather ironically) get another 502 error message whilst trying to serve the 502 error message:

"Additionally, a 502 Bad Gateway error was encountered while trying to use an ErrorDocument to handle the request."

The reasons for this are rather obvious: the request error502.html is also going to be rewritten, and since nothing under http://192.168.131.85:13080 is going to work (because the Zope instance is down!) this produces another 502.

One workaround is to use an absolute URL in the ErrorDocument directive, e.g.

ErrorDocument 502 http://www.openia.com/error502.html

but we didn't like this because this causes a browser redirect, so the visitor loses the URL they entered into their browser, which is very annoying if you want to keep refreshing to find out when the site comes back up. Also the redirect code is confusing for search engines:

"the client will not receive the original error status code, but instead will receive a redirect status code. This in turn can confuse web robots and other clients which try to determine if a URL is valid using the status code."

Therefore we wanted a workaround that avoided this. The solution we came up with is to add a RewriteCond to exclude error502.html thus:

<VirtualHost 89.167.224.29:80>
  ServerAdmin hostmaster@openia.com
  ServerName yourdomain.co.uk
  RewriteEngine on
  RewriteCond %{REQUEST_URI} !^/error502.html$
  RewriteRule ^/(.*) http://192.168.131.85:13080/VirtualHostBase/http/%{SERVER_NAME}:80/plone/VirtualHostRoot/$1 [P]
  DocumentRoot /var/www
  ErrorDocument 502 /error502.html
</VirtualHost>

in words this means "Rewrite all URLs to the zope instance, unless the URL is error502.html". This now works perfectly. Problem solved. It's odd I haven't seen this solution elsewhere on the web.