MoveForward Web Development New Zealand

Blog

Simple .htaccess Redirects For Site Upgrades

Posted by Paul on 29 October 2010 | 0 Comments

Tags:

One thing about content-managed sites is that sooner or later (I prefer sooner rather than later) you're going to need to upgrade the core system behind the site.

Whether it's Wordpress, Expression Engine, Drupal, SilverStripe or any other CMS, the last thing you want during an upgrade is someone using the site at exactly the wrong moment and corrupting the database. You also want to shield your site users from any unwanted site behaviour or disturbances during the process.

Here's how I do it.

1. Find out your IP address

While undoubtedly there are a raft of command-line techniques to do this, the simplest method I've found is to use http://www.whatsmyip.org .

Once you've found out what your external IP address is, make a note of it - you'll need it for the next step

2. Add a "holding page" to your webroot

I use a simple, unstyled page which simply states the domain is undergoing maintenance and we be back online soon. 

MoveForward "Holding Page" 

Obviously you can be as simple or as complex as you like, depending upon the needs of your site, but the bottom line is that it should be a nice, light-weight HTML page, as during the upgrade process all site traffic (except requests from your IP address) will be routed to this page.

3. Add or edit your .htaccess file

Silverstripe and Drupal both use .htaccess files to perform some routing of search-engine friendly URL's into software-friendly paths as well as initiating the framework they se to run the entire site.

Once you know your IP address, you can add something like this to the top of your .htaccess file:

RewriteEngine On

# 'site under maintenance' rules

RewriteCond %{REMOTE_HOST} !^60\.234\.225\.204

RewriteCond %{REQUEST_URI} !/index\.html$

RewriteCond %{REQUEST_URI} !/styles\.css$

RewriteRule .* /index.html [R=302,L]

# end 'site under maintenance' rules

That's quite a lot to chew on, so let's step through it.

There are 3 conditions and one rule.

1. RewriteCond %{REMOTE_HOST} !^60\.234\.225\.204

meaning: "if the user's IP address isn't 60.234.225.204"

2. RewriteCond %{REQUEST_URI} !/index\.html$

meaning: "and the request isn't for the file index.html"

3. RewriteCond %{REQUEST_URI} !/styles\.css$

meaning: "and the request isn't for the file styles.css" (Yes, you'll need to add one of these rule types for every file your holding page will be requesting - including images) 

4. RewriteRule .* /index.html [R=302,L]

meaning: "redirect the user to the file index.html"

4. Cleaning up afterwards

As you'll be able to see and use the site normally, one 'trap for young players' is that the site will appear fine to you but will be in 'maintenance mode' for every other user until the .htaccess rules are commented out. To comment out the lines, add a hash symbol (#) to the beginning of each of the 'holding page' conditions / rules. 

The system above is a simple, effective way to route visitors away from the site during upgrades or maintenance work and has worked like a charm for me during the various Silverstripe upgrades I've run.