Maintenance mode (HTTP 503) with lighttpd and PHP

When you put your website in maintenance mode, it’s a good idea to return a HTTP 503 error code to the client.

This code indicates that “the server is currently unable to handle the request due to a temporary overloading or maintenance of the server”.

The 503 code is used to avoid crawlers or caching proxy use the maintenance page as the new valid content for the request. You certainly don’t want Google save this content in his search index as the content of your website 🙂

To achieve this we will use a rewrite rule in lighttpd to redirect all requests to a single PHP script which will return a 503 error code and print an informative message.

Lighttpd configuration

url.rewrite = ( "" => "/maintenance.php" )

This configuration will redirect any request to maintenance.php script.

If you need to serve an image in your maintenance page, you have to add another rule to the rewrite process like that :

url.rewrite = ( "upgrading.png" => "$0",
                "" => "/maintenance.php" )

Let some users see the website

It might be usefull to let admins or developers access the website during the maintenance.
For that, you can disable the maintenance rewrite rule for certain IP addresses :

 $HTTP["remoteip"] != “192.168.1.42″ {
               url.rewrite = ( "upgrading.png" => "$0",
                      "" => "/maintenance.php" )
}

PHP code example

<?php
header("HTTP/1.1 503 Service Unavailable");
?>
We're currently upgrading our servers...

Links