Apache Catch All Subdomain


apache catch all subdomain

You can always configure multiple subdomains on Apache web server. However, what about the subdomains not listed in your Apache config file? The solution is to create a catch all subdomain to handle all unlisted subdomains on your website. It also helps you create separate subdomains for your clients, if they need it. Let us take a look at how to configure Apache catch all subdomain for your website/application.

 

How to Configure Apache Catch All Subdomain

Here are the steps to configure Apache catch all subdomain for your website/application

 

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 Subdomain

We will use NameVirtualHost and ServerAlias directive to create a catch all subdomain.

Add the NameVirtualHost directive.

Next, add a virtual host block. The key is to use ServerAlias to specify the catchall subdomain. You cannot use a wildcard sub domain in ServerName

 


NameVirtualHost *:80
<VirtualHost *:80>
  DocumentRoot /var/www/wildcard
  ServerName www.example.com
  ServerAlias *.example.com
</VirtualHost>

If the above code doesn’t work for you, you can try using the VirtualDocumentRoot directive in addition to ServerAlias.

<VirtualHost *:80>
    ServerAlias *.example.com
    VirtualDocumentRoot /var/www/%1
</VirtualHost>  

 

The above virtual host will catch all subdomains (blog.example.com, app.example.com, etc) not listed on your website’s config file.

However, if you want to setup multiple virtual hosts, with a few specific ones and a catchall subdomain, you can do so by creating separate virtual hosts for specific subdomains before specifying the one for wildcard subdomain, as shown below

<VirtualHost *:80>
  DocumentRoot /var/www/app
  ServerName app.example.com
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/www/example
  ServerName www.example.com
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/www/wildcard
  ServerName blog.example.com
  ServerAlias *.example.com
</VirtualHost>

In the above case, the catchall subdomain will be matched only when the top 2 virtual hosts don’t.

 

 

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!