Using .htaccess to suspend an entire site while still working on it

Published March 3rd, 2011

Sometimes, when you’re working on a web site, you need to make some changes that could cause problems if there is someone else using the site at the time. The obvious solution is to suspend the site — e.g. put up a ‘this site is down for maintenance’ page — but how do you do this while still being able to view and work on the site yourself?

In fact, it’s really quite easy, using .htaccess. Simply put, you use mod_rewrite to redirect all requests to the maintenance page, unless the user’s browser has a particular cookie.

RewriteEngine On
RewriteBase
RewriteCond %{HTTP_COOKIE} !^.*secret-cookie.*$ [NC]
RewriteRule .* maintenance-page.html [NC,L,R=503]

Clearly you can change the name of the cookie from ‘secret-cookie’ to whatever you like. Its value isn’t important. And of course, you can change the name of your maintenance page.

Update: as suggested by Michael, below, I’ve added the R=503 status code to indicate that the service is temporarily unavailable.

Now you just need to create the cookie in your browser. Personally I use the FireCookie extension for Firebug which makes this very easy.

That’s it!

Get a Trackback link

2 Comments

  1. Michael on August 3, 2011

    You have to send back 503 Service Unavailable HTTP response code unil the end of maintenance.

  2. Stickman on August 3, 2011

    Thanks for pointing this out Michael, I’ve updated the post to include a redirect with the appropriate status code.

Leave a comment

Comment Policy: First time comments are moderated. Please be patient.

OpenID

Anonymous