Planned site downtime: how to handle it with PHP

25th January, 2011 - Posted by david

Today I read an interesting article on Google’s Webmaster Central blog about planned down time, for maintenance or whatever. Basically, you need to communicate to users and potential site-crawlers/indexers that the site is down, but it’s ok, it’ll be back up soon!

My suggestion would be to do a mod-rewrite (if using Apache, but other web servers have equivalents) on all incoming files to your 503 file, something like

RewriteRule ^(.*)$ 503.php [L]

If you put this rule first, you can leave the rest of your rules where they are as the “L” at the end of the line tells Apache that this is the last rule it needs to process, i.e. it can ignore the others.

Then, in your 503.php you need to tell any web-crawlers/site-indexers that the service is down but will be back at a certain time using PHP’s header function, as well as telling your users the same using a plain simple descriptive paragraph:

<?php

$back = 'Tue, 1 Feb 2011 12:00:00 GMT';

// tell any web-crawlers
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Retry-After: '.$back);

// calculate time to wait for users (I assume here it'll only be a matter of hours)
$vals = array('hour' => 3600, 'minute' => 60, 'second' => 1);
$back_in_seconds = strtotime($back) - time();
$back_text = '';
foreach ($vals as $key => $time) {
    $cur_time = floor($back_in_seconds / $time);
    if ($cur_time) {
        $back_text .= $cur_time.' '.$key;
        if ($cur_time != 1) $back_text .= 's';
        $back_text .= ', ';
        $back_in_seconds %= $time;
    }
}
$back_text = rtrim($back_text, ', ');
?>
<html>
   <!- etc. -->
    <body>
    <p>
        We are currently undergoing scheduled downtime to upgrade the site. <br />
        We'll be back in <?= $back_text ?>. Thanks for your patience.
    </p>
    <!-- etc. -->
    </body>
</html>

Should all be pretty straight-forward, but it’s good to let people know that you know you’re down and when you expect to be back.

Tags: google php | david | 25th Jan, 2011 at 22:58pm | No Comments

No Comments

Leave a reply

You must be logged in to post a comment.