Apache and Lighttpd server send error 503 when web site is under maintenance
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.
Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
You may also be interested in other helpful articles:
- Poll: Your Remote Maintenance Services
- Linux missing GPG db42a60e key error message
- nixCraft FAQ Roundup ~ Sep, 24, 2007
- Poll: Your UNIX / Linux Admin Automation Scripting Language
- Lighttpd Control a Directory Listing With mod_dirlisting
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: apache_htaccess, database_driven_site, error_503, http, Linux, maintenance, mod_rewrite, rewrite_server_software, search_engine, web, webdesign




Good tips, but if the site is really important then the step on Restarting Apache 2 web server after saving and closing the file needs to verify that the syntax of the configuration file is correct. There are many ways mistakes or typos can be introduced, any of which will bring the server down.
# /etc/init.d/httpd restart
or
# /etc/init.d/apache2 restart
One should do a configuration test first. e.g.
# /etc/init.d/apache2 configtest && \
# /etc/init.d/apache2 reload
or
/usr/sbin/apache2ctl -t -f /etc/apache2.conf && \
/usr/sbin/apache2ctl -f /etc/apach2.conf graceful
Good idea; For lighttpd
lighttpd -t -f /etc/lighttpd.conf && /etc/init.d/lighttpd restartAppreciate your post.
Do you need to restart Apache after modifying .htaccess? I think not. I you were changing httpd.conf I would agree.
I’m just learning to hack .htaccess and use mod_rewrite. It looks to me like this line:
RewriteRule !down.php$ /down.php [L]
only catches URLs to the root directory? How can I redirect all URLs, even URLs to subdirectories deeper in the site, so all URLs get a 503 maintenance page?
Thanks.
Jerry,
No .htaccess rule is correct. If file is not down.php it will display content of down.php for all site urls and sub directories.
Hope this helps.
Hi, i am getting the error 503 when i am importing the data to the client side.i am really not understanding wats the reason behind this.So please can any one help me out to solve this problem.Thanks in advance….
Hi,
Thats very efficient way of costimising error pages.
Do you know the way to do the same on tomcat alone with out web server. ie; with out apache.
I use only tomcat 5.5.12 and I want to customise my 503 pages when I am updating.
Thanks in advance.
I have tried to use your method, but only the main page and a page one level deeper redirect successfully. If I go any deeper down a valid URL it does not display the site down page.
Do you have other rewrite rules? if so add this rule before those rules