ComputersGeneralLinuxWeb DesignWeb Site

Website maintenance alternate page using .htaccess

In an ideal world your website would be up 24 hours a day 7 days a week. You’d perform upgrades on an identical system and seamlessly cut over without any customers noticing, and that would be available at no extra cost. Back in the real world and you soon discover that the cost of having a second server and of the other high-availability features puts this out of the reach of most smaller websites.

An upgrade in progress message is not ideal, but it’s cheap, easy and is better than embarrassing error messages.

When upgrading WordPress the recommended procedure is to disable plug-ins and then delete some of the folders before uploading the new code. This means that the site can look pretty bad during the upgrade. This results in badly formatted pages, unsightly code and script error messages.

To make it a little better during my recent upgrade I redirected users to a temporary page using the .htaccess file and mod_rewrite module.

These are the steps that I performed to provide a temporary site unavailable page during the upgrade. More details of the WordPress upgrade procedure.

Offline page using .htaccess during wordpress upgrade

One of the challenges when taking part of a site offline is identifying what pages need to be redirected to the temporary page. Previously I only used wordpress for my blog, so I could just redirect the blog section and leave the rest of the site as normal, but since I now use WordPress for my entire site that is no longer possible.

Once I’d decided to redirect the entire site some potential problems arose.

Upgrade in progress message also redirected
One was that I couldn’t actually display the upgrade in progress page as it would also get caught by the same redirect and end up in a perpetual loop. Thankfully this issue is easily resolved with an additional rule in the .htaccess file (see later).

No CSS / images
The other problem is that the page is made up of several images and css files which are required to maintain the formatting. Whilst it is possible to redirect just the pages / posts and leave the images and some CSS files available for use in the not available page this would have needed a much more complex set of rules and I decided it wasn’t worth going to so much effort for the 30 minutes or so that the site would be unavailable. An alternative would be to provide the CSS and images from another site, but again I didn’t think that it was worth it for such a short period of time. I therefore used a very basic text only html file to provide the upgrade in progress message.

Need administrator access
Another challenge is how to access the administrator pages website during the upgrade which is required to perform the upgrade (eg. make any changes to the database) and to re-enable the plug-ins before making the site live again. Again it would be possible to create a complex ruleset to get around this, but instead I just used a simple rule to stop my computer from being redirected so that I could still get to the administrator pages.

The first step is to create and upload the temporary page. In this case I used a very basic text only html page which I named offline.html.

The following was then added to the .htaccess file in the top level of the website. More details on using mod_rewrite on Apache.

RewriteEngine on

# Temp maint mode for wordpress upgrades
# set IP address appropriately
RewriteBase /
RewriteCond %{REMOTE_HOST} !^123\.123\.123\.123
RewriteCond %{REQUEST_URI} !/offline\.html$
RewriteRule .* /offline.html [R=302,L]

This works as follows:

  • enables the rewrite rules (this was included in my .htaccess file already)
  • sets starting position as the top level
  • excludes my IP address (replace 123.123.123.123 with your own IP address)
  • excludes the offline.html file from the rule (otherwise you end up in a perpetual loop – not a good thing)
  • then replaces anything on the URL with /offline.html

The use of response code 302 is to indicate a temporary redirect.