WP-Mix

A fresh mix of code snippets and tutorials

503 Service Unavailable Headers via PHP

Sending a few HTTP headers is a simple way to let visitors and search engines know that your site currently is unavailable. This is useful when you are doing maintenance and don’t need access to the front-end of your site. Simply add the rules when your site is down, and then remove them once everything is ready to go live.

Send 503 headers

Here is the magic code:

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

No code modifications are required, simply plug-&-play. Once in place, this code sends the appropriate 503 “Service unavailable” headers, along with a Retry-After header to let bots and search spiders like Googlebot know when to try the site again. Sending 503 headers for site maintenance is optimal because it tells visitors that the downtime is temporary and the site will back online soon.

Note: as-is, the code sends a Retry-After header set to 3600 seconds, which is equivalent to 60 minutes. You can customize this by changing the value to whatever is required (in seconds).

Redirect specific bots (via .htaccess)

It’s also possible to redirect specific bots to a custom page. For example, if we want to send googlebot to a custom page at /googlebot.php, we can add the following code to our site’s root .htaccess file.

# Redirect googlebot
<IfModule mod_rewrite.c>
	RewriteCond %{HTTP_USER_AGENT} ^(.*)google(.*) [NC]
	RewriteRule (.*) /googlebot.php [R=503,L]
</IfModule>

To target other search-engines, check out my list of all user agents for top search engines. Also check out the related resources listed below for more information and tips.

Related

Learn more

.htaccess made easy