≡ Menu

webdesign

When you are updating website you may need to send error 503 to client. Error 503 indicates that web server is temporary out of service or down. This is useful if you are running popular database driven website such as a forum or e-commerce site. So when the site is under maintenance you can send user a good message indicating that site is down for some work.

This tip will help you to disable a site for maintenance using mod_rewrite without redirecting url.

Both Lighttpd and Apache webserver allows you to send this message to client using server side rewriting using mod_rewrite and php.

Error 503 means

=> Server is too busy to serve your request
=> Server is slashdotted or dugg to death
=> Server is forced to send this message etc

But why to send error 503?

Error 503 informs search engine that site is temporary out of service. This is quite important for site which is heavily depends upon search engine for selling products and services.

PHP code - down.php

The down.php file simply sends a 503 HTTP error header indicating that service is unavailable. echo command sends a message for your site.

So first create a php file as follows:
<?
header("HTTP/1.1 503 Service Unavailable");
echo "Server is down for maintenance. We will be back in 60 minutes.";
?>

Task: Lighttpd send error 503 when web is under maintenance

Copy down.php to webserver documentation root:
cp down.php /home/lighttpd
OR
cp down.php /var/www

Open lighttpd configuration file
# vi /etc/lighttpd/lighttpd.conf
Now add following code:

$HTTP["url"] != "/down.php$" {
   url.rewrite = ( "" => "/down.php" )
}

Save and close the file.

It is also possible to block site for rest of the world except for website developer or site admin IP address:

$HTTP["remoteip"] != "201.203.149.149" {
                   url.rewrite = ( "" => "/down.php" )
}

Restart lighttpd:
# /etc/init.d/lighttpd restart

Task: Apache send error 503 when web is under maintenance

You can use an Apache mod_rewrite module. Open .htaccess file
vi .htaccess
Append following code:
RewriteEngine on
RewriteRule !down.php$ /down.php [L]

Again it is possible to block site for rest of the world except for website developer or site admin IP address:
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^201\.203\.149\.149$
RewriteRule !down.php$ /down.php [L]

Save and close file. Restart Apache 2 web server:
# /etc/init.d/httpd restart
OR
# /etc/init.d/apache2 restart

Let us say user hits url http://domain.com/dir/somefile.html
First rules will check for client IP address - 201.203.149.149. If match found user is allowed to browse site. Otherwise user will get 503 error with message:

Server is down for maintenance. We will be back in 60 minutes.