Jan 08 2009

GoogleBot a HTTP status 503

Tag: PHP,SEO,Zend FrameworkJens @ 23:55

Občas je třeba provést na stránkách údržbu tak, že je třeba na chvíli zastavit zobrazování webu – ať už je to údržba databáze nebo cokoliv jiného. V takovém případě se změní například index.php aby zobrazoval „nějakou hlášku o údržbě“. Problém ale může nastat, když zrovna v tu chvíli indexuje váš obsah třeba GoogleBot. Vaše hláška o údržbě pro něj bude obyčejná informace vrácená typicky s hlavičkou HTTP statusu 200 (viz. RFC HTTP status 200) a GoogleBot stránku pěkně zaindexuje a pak se nestačíte divit co že to google vrací za výsledky na vašem webu :)

Na toto existuje doporučení přímo od Googlu: „… nastavte server tak, aby vrátil status 503 (viz. RFC HTTP status 503) namísto statusu 200 …“. V PHP jednoduchá věc pomocí funkce header(), ideálně ještě doplníme vlastní hlášku o údržbě:

<?php
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
?>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>503 Service Temporarily Unavailable</title>
</head><body><h1>Service Temporarily Unavailable</h1>
<p>The server is temporarily unable to service your request due to maintenance downtime or
capacity problems. Please try again later.</p>
</body></html>

Retry-After hlavička

Navíc, podle RFC – Header Field Definitions je může být použito v hlavičce u statusu 503 pole Retry-After:

The Retry-After response-header field can be used with a 503 (Service Unavailable) response to indicate how long the service is expected to be unavailable to the requesting client. This field MAY also be used with any 3xx (Redirection) response to indicate the minimum time the user-agent is asked wait before issuing the redirected request. The value of this field can be either an HTTP-date or an integer number of seconds (in decimal) after the time of the response.
Retry-After = "Retry-After" ":" ( HTTP-date | delta-seconds ) Two examples of its use are Retry-After: Fri, 31 Dec 1999 23:59:59 GMT Retry-After: 120 In the latter example, the delay is 2 minutes.

Tedy, navíc ještě přidáte hlavičku Retry-After s hodnotou (v sekundách, nebo přímo časem), za kterou předpokládáte že web pojede – na příklad za 1 hodinu (3600s):

header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 3600');

Zend Framework

A závěrem ještě příklad, pro odeslání statusu 503 v Zend Frameworku:

$this->getResponse()->clearBody();
$this->getResponse()->setRawHeader('HTTP/1.1 503 Service Temporarily Unavailable');
$this->getResponse()->setRawHeader('Status: 503 Service Temporarily Unavailable');
$this->getResponse()->setRawHeader('Retry-After: 3600');

$msg = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">';
$msg.= '<html><head><title>503 Service Temporarily Unavailable</title>';
$msg.= '</head><body><h1>Service Temporarily Unavailable</h1>';
$msg.= '<p>The server is temporarily unable to service your request due to maintenance downtime or
capacity problems. Please try again later.</p>';
$msg.= '</body></html>';			

$this->getResponse()->setBody($msg);
$this->getResponse()->sendResponse();