Diverting all requests to a 503 holding page
2007-09-22 14:43:55
While the best practice is to avoid downtime wherever possible, it may not always be practical -sometimes it is necessary to take a website, or part of a website, off-line while carrying out updates (e.g. when upgrading some types of blog/forum software). While performing such maintenance a good idea is to redirect all page requests to a holding page. This page will inform users of what is going on, and can also be used to tell search-engine spiders to come back later.
Creating the holding page - 503.php
The holding page can be as simple or as complex as you require. In most cases
a simple page is probably the best option (less to go wrong). The page will
feature some PHP code to return the 503 status message, and a meta tag just
in case. The following is a sample holding page.
<?php
header("HTTP/1.1 503 Service Temporarily Unavailable");
header("Status: 503 Service Temporarily Unavailable");
header("Retry-After: 3600");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Site Off-line</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="robots" content="none" />
</head>
<body>
<strong>This website is off-line for maintenance, please check back soon...</strong>
</body>
</html>
You can include other files, such as images, in the holding page, but you will need to enable them as appropriate when creating the htaccess file later. The PHP code returns a 503 error, which most bots will understand and so will ignore the page for a later visit. For those bots that do not understand the header the meta tag should ensure they dont overwrite your pages with the holding page.
Creating the .htaccess file
The way we serve the holding page is to use htaccess and mod_rewrite. This is
not a redirect since users are not forwarded to another page. More accurately
the URL is rewritten and shows up the page specified. The following is a sample
htaccess file suitable for this task.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^12\.34\.56\.78
RewriteCond %{REQUEST_URI} !^/503.php [NC]
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png)$
RewriteRule .* /503.php [L]
Since you will need to be able to check your upgrades are working, or run installation scripts from your site while the holding page is up we will need to make an exception for your IP addresss. Line 4 deals with this ("if the user is not on this IP run the rewrite rules"). Replace the numbers given with your current IP address (which must remain static for the session).
Line 5 ensures that we dont get stuck in an infinite loop (ie, if the page requested is 503.php dont redirect to 503.php). Any other request will rewrite to 503.php.
Line 6 allows you to use images (with the gif, jpg or png extension) in your holding page - you could allow other extensions (e.g. CSS, JS) if you need them in your holding page, or remove this line completely if you only want the PHP holding page to show.
The final line is the bit that redirects requests the the holding page. The [L] on the end means that if that rewriterule is processed make it the final rule. This effectively means that you can test your own htaccess rules (by placeing them below it) without interfering with the holding page.
Enabling the holding page
Enabling the holding page is very simple. First, upload 503.php to the root
of your public HTML area (you may wish to leave it there for future use). To
enable your holding page simply switch your existing htaccess file (if you have
one) for your holding page htaccess file. This will affect subdirectorys also.
To disable the holding page simply reverse this process by switching the htaccess
files back.
Tip: You may want to check your holding page works, but cant since your IP address is allowed access anyway. A good, simple way to check that your holding page is working is to run your page through an online proxy (or an online page translator, such as Google Translate). This should result in the request coming from an IP that is not your own, triggering theh holding page.
Tip 2: The htaccess file will affect all subdirectorys below it as well, but
not those above it. This means that you can systematically disable only the
parts of your site that need to be offline. For example, say you had a directory
called "forum", with the rest of the site in the top level directory.
You could place the htaccess file in the forum directory to disable just the
forum whilst upgrading it.
