How to create a maintenance page for Apache web server

Apache web servers have been a part of my life for nearly two decades. While Apache and the web has certainly changed a lot since 1995, one of the things that never changes is that at some point I’m going to need to toss up a maintenance page for one of my websites.

Doing a quick patch to a website, might not require bringing the site offline, but if there’s any sort of data munging happening, it’s better to be safe than sorry.

Fortunately, throwing up a maintenance page for any site is easy if you’re running Apache. It’s probably easy on other web servers too, but I only use Apache.

I just add the following code to a site’s .htaccess file or virtual host configuration.

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/maint.html -f
RewriteCond %{DOCUMENT_ROOT}/maint.enable -f
RewriteCond %{SCRIPT_FILENAME} !maint.html
RewriteRule ^.*$ /maint.html [R=503,L]
ErrorDocument 503 /maint.html

To activate the maintenance page I just have create a file called maint.enable in my web server root.

touch maint.enable

In the maint.html page I put something really simple like this – updating the date and time manually.

<!DOCTYPE html>
<html>
 <head>
 <title>Maintenance</title>
 </head>
 <body>
 <h1>Scheduled Maintenance</h1>
 <p>November 18, 2013 11:49 PM Eastern</p>
 <p>Thank you for stopping by. Unfortunately we're in the middle of a scheduled upgrade and we'll be offline for a brief period.</p>
 <p>Please check back later.</p>
</body>
</html>

I think the date and time is important to include in the maintenance message because it gives the user a sense that this maintenance is happening right now. Otherwise, users might think this is how my site has been for 2 months or 2 years.

And when I’m done with my maintenance, I just delete the maint.enable file.

rm maint.enable

What I like about this technique is that I can enable and disable the maintenance page without restarting or reloading Apache.

I’m sure I didn’t invent this technique. Unfortunately I have no idea where I may have stumbled across this fairly simple and elegant solution. Posting this here is just my way of spreading the knowledge.

I hope this helps someone.

Cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *