Maintenance mode with apache mod_rewrite

With small websites sometimes you need to put up a maintenance page while you are making some modifications. Typically you should have some staging enviornment but sometimes infrastructure does not allow for this. If you find your self in this situation you might like this tip.
It is not sufficient to just replace your index with a maintenance page for multiple reasons. First what about sub pages? Without a redirect on those people will be able to access other areas of your site which may of may not be desireble.

Using mod_rewrite you can be friendly to search engines, and your users.

Try this in your .htaccess

ErrorDocument 404 /maint.html
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maint.html$
RewriteCond %{REQUEST_URI} !/maint.jpg$
RewriteRule $/maint.html$ [R=302,L]

This will give a 302 (Moved Temporarily) msg to clients and serve
maint.html. maint.html can have content similar to this.

<img src="/maint.jpg" alt="We are currently undergoing maintenance. We will be back shortly">'

The leading slash on the image is important as it will serve the image even if someone accesses http://yourdomain/somethig/somethingelse.

Its also important to set your 404 as your maint.html so anyone accessing pages that do not exist get the same maintenance content.

You might want to add a condition like the following if you want to be able to browse your website normally while in maintenance mode.

RewriteCond %{REMOTE_HOST} !^123\.123\.123\.123

Be sure to replace 123.123.123.123 with your ip address. This will cause apache to not apply the rule to the specifed ip.

Hope someone finds this useful. I know I have used it several times in the past.

2 Comments

  • This is more elegant that what I’ve done before.

    In the past, I would have a maintenance version of my httpd.conf file and then reload Apache with that config when needed. In the conf, I would specify a new doc root directory with the maintenance page as the index page, and then point all 404’s to the maintenance page as well. (which is kind of ugly)

    In the past, we’ve also configured our load balancers with maintenance servers that would eclipse normal web serer traffic when the maintenance web services were running.

  • this just doesn’t work. the rules are wrong.

    RewriteCond %{REQUEST_URI} !^/maint.html
    RewriteRule .* /maint.html [R=302,L]

Leave a Reply

Your email is never shared.Required fields are marked *

To submit your comment, click the image below where it asks you to...
Clickcha - The One-Click Captcha