≡ Menu

Return 503 when server is under maintenance

When your server is under maintenance like you are upgrading your WordPress install or you are moving some pages around, it is imperative that you return the correct HTTP status code when the maintenance is going on. The correct HTTP status code to return is 503. If you return a 404 page not found, search engines like Google will immediately remove the page from the index and it can take months for the page to get back into the index. Returning any other code other than 503 should be avoided.

RFC 2616 Section 10 gives the following definition of a 503 error code:

The server is currently unable to handle the request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay. If known, the length of the delay MAY be indicated in a Retry-After header.

The Retry-After header is also quite important. It gives the duration of the maintenance window in seconds.

In PHP, you can return a 503 status code very easily using the header command:

header('HTTP/1.0 503 Service Temporarily Unavailable');
header('Retry-After: 300');

The first statement returns the 503 status code and the second statement instructs the search engine to attempt re-crawl after 300 seconds (5 minutes). You may want to increase or decrease this interval depending on how much time you think your server maintenance is going to take.

You must return the 503 error code in all of your pages. So it might be beneficial to be able to include a header.php file in all your php files which then returns the status code.

On normal operating conditions, your pages generally return a 200 status code which means that the page is present on the server and was able to successfully return the response.

Commonly used HTTP error codes:

200: Status OK. The page was found and ran successfully
404: Page not found. This status code should only be used for pages that have been permanently deleted. Don’t use this status code while doing server maintenence.
503: Service temporarily unavailable. Use this status code when the server is temporarily unavailable because of administrative maintenance.
500: Internal Server Error. This error code means that there was some error while running your scripts and this was an abnormal termination of the script. The most common reason for this error is a syntax error in your .htaccess files. You never need to send this error programmatically.

{ 0 comments… add one }

Leave a Comment