Apache Catch All VHost


apache catch all vhost

Apache allows you to configure multiple virtual hosts on a single server. However, what about the virtual hosts that are not listed in your server config file? It is advisable to setup a catch all VHost that handles all unlisted virtual hosts. Let us take a look at how to configure Apache catch all VHost for your web server.

 

How to configure Apache Catch All VHost

Here are the steps to configure Apache catch all VHost.

 

1. Open Apache config file

Open Apache config file in a text editor. You will find it at one of the following locations:

  • /etc/apache2/httpd.conf
  • /etc/apache2/apache2.conf
  • /etc/httpd/httpd.conf
  • /etc/httpd/conf/httpd.conf

 


$ sudo vim /etc/apache2/httpd.conf

 

2. Setup Apache Catch All VHost

Let’s say you run 2 domains www.domain1.com and www.domain2.com and want to setup a catch all virtual host. We will use NameVirtualHost and ServerAlias directive to run both virtual hosts on same ip and port.

Add the NameVirtualHost directive to your .htaccess file.

Next, add 2 virtual host blocks, one for each virtual host , as shown. They both run on port 80.


NameVirtualHost *:80

<VirtualHost *:80>
 ServerName www.domain1.com
 DocumentRoot /var/www/html/domain1
 ErrorLog /var/log/apache2/domain1-error.log
</VirtualHost>

<VirtualHost *:80>
 ServerName www.domain2.com
 DocumentRoot /var/www/html/domain2
 ErrorLog /var/log/apache2/domain2-error.log
</VirtualHost>

 

When we use NameVirtualHost directive, the first virtual host is loaded as default, and is used to handle all unmatched domains, that is, as a catch all virtual host. In this case, you don’t need to setup a separate virtual host to handle unmatched domains.

 

If the above code doesn’t work for you, or you specifically want to keep a separate catch all virtual host, then use ‘*’ as its server name.

 


NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot /www/docs/domain1
ServerName domain1
...
</VirtualHost>

<VirtualHost *:80>
DocumentRoot /www/docs/domain2
ServerName domain2
...
</VirtualHost>

<VirtualHost *:80>
DocumentRoot /www/docs/catchall
ServerName *
...
</VirtualHost>

 

 

Please Note: If you have setup separate virtual host files for each domain in Apache, then update the virtual host config files, with the ones shown above.

 

3. Restart Apache web server

Restart Apache web server to apply changes


$ sudo /etc/init.d/apache2 start [Debian or Ubuntu]
# sudo apachectl restart [RHEL, CentOS or Fedora]

 

Please ensure that you have cleared the browser cache before you try loading the page again

 

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!