I have an Apache2.2 webserver set up to handle dozens of virtual host sites for our organization. I am looking for a simple method to temporarily 'turn off' all the sites by having all site requests redirect to a single "server maintenance mode" page (rather than a 404 File not found message). Is there possibly a way to do this using a mod_rewrite rule in the apache2.conf file? This would save me from having to 'touch' all these sites and turn them off individually when making changes on the server that will affect all sites.

The server setup and structure:
In the apache2.conf file the primary website is declared in the section

<VirtualHost *:80>  
ServerName mainsite.org  
DocumentRoot /var/www/main_site   

All the other sites on the server are subdomains (abc.mainsite.org, news.mainsite.org, etc) and use standard vhost files set in /sites-available/ with a symlink in /sites-enabled/. They are activated/called by the line in apache2.conf following the VirtualHost declaration

Include /etc/apache2/sites-enabled/

I can do this for the mainsite.org by uncommenting a rewrite rule in the section that reads:
RewriteEngine on
RewriteBase /

#   Redirect site requests to a 'maintenance' page  
RewriteCond %{HTTP_HOST} ^www\.mainsite\.org  [OR]  
RewriteCond %{HTTP_HOST} ^mainsite\.org  
RewriteRule ^/?(.*)$ http://kubrick.fpgsm.org [R,L]   

[kubrick is the host servers name which is also placed in the 'default' vhost file as follows:]

ServerName kubrick.fpgsm.org  
DirectoryIndex index.html  
DocumentRoot /var/www   

[index.html is where I have the 'maintenance' message.]

I have tried placing a simple rewrite rule in the which is placed above the section, but this does not work. So clearly I am missing something with regards to how the directives in apache2.conf work and/or the relationship between the VirtualHost in apache2.conf and subsequent VHost files.

RewriteCond %{HTTP_HOST} ^([a-z]+)\.mainsite\.org  [NC]  
RewriteRule ^/?(.*)$ http://kubrick.fpgsm.org [R,L]  

Any solutions/suggestions out there? Thanks.

1 Answer 1


I haven't found a way to put in a rewrite rule that puts all your virtual hosts into maintenance mode. Here are some other possible approaches:

From How to disable all apache virtual hosts?, Vinko Vrsalovic suggests using a single command to disable all your virtual hosts:

 find /etc/apache2/sites-enabled/ -type l -exec rm -i "{}" \;

You would then have to create a single virtual host (maybe in /etc/apache2/sites-available/maintenance.conf), use sudo a2ensite maintenance to enable it, then restart or reload apache. When you were done with the maintenance, you could re-enable all your virtual hosts using the a2ensite command.

From Shutting down web server for maintenance, coredump suggests:

use iptables to redirect all traffic on the http port to another port on the server, where you can run a secondary webserver (lighttpd or nginx or anything really small that can serve static html pages). The rule would be:

iptables -t nat -A PREROUTING -p tcp -i ethX -d <server ip> –dport 80 -j DNAT –to <server ip>:<new port>

You can also redirect to another server/port completely.

From Automated Apache Server Maintenance Page, Dan suggests:

It's pretty easy to start up Apache with a custom config file. On my system:

httpd -f <config>

I use this regularly in combination with a shell script an an extremely simple config as a maintenance placeholder. The script simply stops my regular Apache, and starts this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.