| maymay ( @ 2008-08-10 04:24:00 |
| Entry tags: | crosspost, howto, tech/computing, web design |
How to use mod_rewrite rules to easily enable web site “maintenance” modes
When you’re administering a web site, sometimes you need to make changes that for whatever reasons require that the web site be temporarily unavailable for normal visitors. One obvious example is database maintenance. Unless you have the resources to do full-blown load balancing across a server cluster, you probably have to accept that your site is going to be down for a short period of time.
When this happens, it’s generally a good idea to show your visitors a web page that briefly explains the situation. This is typically a page that politely explains that the “site is temporarily down for maintenance” and so on. I’ve taken to calling such a page a “curtain” because it’s a little like putting a curtain up in front of construction work.
You put the curtain up, do whatever you need to do to fix or upgrade or maintain your web site in the background, then take the curtain down. For delicate servers, this has the added benefit of dramatically reducing server load while you do your maintenance tasks. You can even allow access to specific visitors, such as QA testers or remote admins while this curtain is up, while still redirecting normal users to the “down for maintenance” page.
Obviously, the first thing you need is the page that explains your site is down for maintenance reasons. This can be anything you like, but it’s simplest to make it a static HTML page and place any and all resources you need for this page (like images) into the same folder. I often use a directory named down-for-maintenance and I place an index.html file in that folder to use as my “curtain” page. Images go straight into the down-for-maintenance directory, too.
Once you have that, you can then use the following Apache configurations to create an “on/off switch” for putting your curtain up and taking it down.
# To take the web site into a maintenance mode, create a file named
# maintenance-mode-on at the document (website) root, such as this:
#
# touch maintenance-mode-on
#
# To bring the web site back, remove or rename the file, such as this:
#
# mv maintenance-mode-on maintenance-mode-off
#
# To enable the site for your IP address only but nobody else's
# uncomment the second RewriteCond directive.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/maintenance-mode-on -f
#RewriteCond %{REMOTE_ADDR} !^your.IP.address.here$
RewriteRule !^down-for-maintenance/.*$ /down-for-maintenance/ [R,L]
</IfModule>
What this does, step by step, is:
- Determines whether or not you have
mod_rewriteenabled. If you don’t, then nothing happens. - Enables the
mod_rewriterewriting engine. - Checks for the existence of a file called
maintenance-mode-onat your site’s root. If such a file does not exist, nothing happens. - With the second
RewriteConddirective uncommented, it also checks the visitor’s IP address and if it does match the one listed nothing special happens. If it does not match the one listed, the next line, which is the redirect, is executed. - Checks the requested URI and if it does not begin with
down-for-maintenance, a temporary redirect (HTTP status code 302) is issued that points browsers to thedown-for-maintenancedirectory you created earlier. Obviously, if you named this directory something else, you should change thisRewriteRule.
This isn’t perfect. For example, if a visitor is filling out a multi-page form then they might get interrupted half way through when you enable the curtain since the curtain takes effect starting at the next HTTP request after you enable it. That said, the only way to do truly graceful maintenance with zero downtime is load balancing, and that is beyond the capability of most simple sites, but this curtain is extremely simple and extremely effective.