How to take a site down for Maintenance
June 26th, 2008During the night a website I had been working on for a friend needed some hefty changes to the database, involving optimisation, security-related bug fixes and integration with new scripts. The problem was that this was early in the morning, coming up to the peak time that users would want to use the websites’ services. In ultimately naive fashion users were told that the website was ready to be used for this working day and the last thing my friend needed was his boss on the phone shouting obsenities at him.
We decided that the day was still young and that these changes would need to be done whilst the entire site was down. The morning was the best time for this as it would be when staff members would be coming into work, so at best an hour of downtime would be recognised. Many of the scripts being used by the website were easy to turn off, but an entire website wasn’t. There was no ability to press a button to take the website offline and to greet members with a downtime error, so it was up to me to work on this whilst my friend worked frantically with the Oracle console (for those wondering the website is powered by PHP and Oracle).
When taking down a website for maintenance there are a number of factors that need to be considered. For most websites today replacing an index file isn’t the way to go, as users will find their way in through alternative pages through bookmarks and Google. With this being the case we need a method of allowing only one person to view a website, whilst showing all other users a downtime message.
In my case on all of the pages a config.php file was included, making these lines of code sufficient for my needs.
/* Start Maintenance Code */ if($_SERVER['REMOTE_ADDR'] != "127.0.0.1") // Replace with own IP Address { header("Location: /maintenance.php"); // Replace with own maintenance page } /* End Maintenance Code */
This code firstly asks if you are 127.0.0.1. If you are then you can view the website as you please, and if not you are directed to the maintenance.php page.
Another method I had toyed with was using a .htaccess file to do the same thing, although I came across mixed results when using this method. For those who want to give it a go here’s the code anyway.
order deny,allow deny from all allow from 127.0.0.1 ErrorDocument 403 maintenance.php
This code is fairly straightforward for anyone to understand, although the last line could use some explaining. The 403 part represents the 403 forbidden HTTP status code, which tells users that they were able to communicate with the web server, but the website has restricted access for a given reason.