CSS: Providing seven essential vitamins and minerals since 1996.
Have a steaming hot, delicious slice of Standards.

Creating a Custom Downtime Message with mod_rewrite

Creating a Custom Downtime Message with mod_rewrite

August 9, 2008 by Richard

mod_rewrite is a very useful, and very complicated Apache module that lets you manipulate incoming requests.  Uses for this module vary from redirecting content from one URL to another for the purpose of maintaing SEO and legacy URLs to simply making your URLs more readable and understandable, and not so weighed down with technical luggage, as it were.

mod_rewrite's documentation is pretty cryptic, but in that page lies most of what you need to know to use it. Though, albeit, you may have to read that page twenty or thirty times to truly understand how to use it.

One use I've discovered that lends itself well to mod_rewrite's magic, is redirecting traffic during a period of planned downtime.  In the perfect world we'd never have to deal with downtime, but once in a while it just can't be avoided.  The following technique allows you to look more professional while you are down, and gives you a chance to let your users know that the downtime is temporary.

There are a few ways that you could approach this, one method is to use a custom 404 error document, but that method may not be so good, since your HTTP server automatically sets the 404 response code when serving that document, and that can be bad for SEO.  Instead, you want content to appear to exist, but you want all incoming requests to be routed to your downtime message.

The Apache configuration required to do this is surprisingly simple:

RewriteEngine On #RewriteLog logs/rewrite.log #RewriteLogLevel 0 RewriteRule ^(.*)logo.gif$ /images/logo.gif [L] RewriteRule ^(.*) /index.downtime.html [L]

The preceding configuration can go inside a Virtual Host container, or it can appear in the main httpd.conf configuration file in lieu of any virtual host configuration, which will cause all traffic for all virtual hosts to encounter your custom error message.

There are two rules in play here: The first looks to see if the request includes the file name for your logo, logo.gif, which is stored at /images/logo.gif which is the path from your DocumentRoot.  Without this rule, you won't be able to include your logo in the downtime message, since the request for your logo would result in your downtime error message being displayed.  The second rule translates every path that comes in, that doesn't end with "logo.gif" to /index.downtime.html, which again is the path from your DocumentRoot.

Two of the configurations have been commented out, which allow for the specification of an error log, and the error level for that log.  The path that you specify for the log must exist, of Apache will fail to start with an error.  For more information on the log and the log level, see the mod_rewrite documentation.

 

Leave a Comment

Simple HTML is allowed: <code>, <b>, <i>, <u>, <var>, <strong>, <em>, <blockquote>, <ul>, <ol>, <li>, <p> (no attributes). All spacing and line breaks in your comment will be preserved.

Archive

PHP Powered XHTML 1.0 CSS
Copyright © 1998-2012 Richard York, All Rights Reserved.