Sometimes you may need to create virtual host for wildcard subdomain. This is useful if your clients ask you to provide separate subdomains for them, or you need to white-label your application. In this article, we will look at how to create wildcard subdomain virtualhost in Apache server.
How to Create Wildcard Subdomain VirtualHost in Apache Server
Here are the steps to create wildcard subdomain virtualhost in Apache server.
1. Open VirtualHost Configuration
If you have configured virtualhost configuration file for your domain e.g. example.com at /etc/apache2/sites-available/example.conf open it in a text editor.
$ sudo vi /etc/apache2/sites-available/example.conf
Otherwise, you may open the default virtual host configuration file in a text editor.
$ sudo vi /etc/apache2/sites-available/000-default.conf
2. Add Wildcard Subdomain
Let us say you want to serve wildcard subdomains’ files from /var/www/html folder. For example, say, you want to serve blog.example.com from /var/www/html/blog, dev.example.com from /var/www/html/dev and so on. In this case, just add the following lines to your Virtual Host configuration file. Replace example.com below with your domain and /var/www/html with the location of Document Root for wildcard subdomains.
<VirtualHost *:80> ServerAlias *.example.com VirtualDocumentRoot /var/www/html/%1/ </VirtualHost>
In the above code, the VirtualHost tag listens to port 80. The ServerAlias directive ensures that your Apache server listens to all subdomains for your domain, because we have used * wildcard character in it. Please note, you can use * only with ServerAlias directive and not ServerName.
The VirtualDocumentRoot specifies the root folder for subdomains. Please note, we have used %1 to specify the subfolder that corresponds to subdomain name. So when a user requests blog.example.com, %1 will be “blog” and Apache will look for its files in /var/www/html/blog subfolder.
If all your wildcard files are in the same folder e.g /var/www/html/wildcard, replace the above code as shown.
<VirtualHost *:80> ServerAlias *.example.com DocumentRoot /var/www/html/wildcard/ </VirtualHost>
In this case, you can do with DocumentRoot and you don’t need VirtualDocumentRoot.
You may also use wildcard subdomain virtual host in conjunction with virtual hosts for other subdomains. Here is an example, where we have separate virtual host for blog.example.com and we also have a wildcard subdomain virtual host to catch all other subdomain requests.
<VirtualHost *:80> DocumentRoot /var/www/blog ServerName blog.example.com </VirtualHost> <VirtualHost *:80> DocumentRoot /var/www/example ServerName example.com </VirtualHost> <VirtualHost *:80> DocumentRoot /var/www/wildcard ServerAlias *.example.com </VirtualHost>
3. Restart Apache server
Restart Apache server to apply changes.
$ sudo service apache2 restart
How to Block Referrer Spam in Apache Server
How to Fix Upstream Closed Prematurely Error in NGINX
How to Serve Static Files from Different Folder in NGINX
How to Pass Parameters in Shell Script
How to Restrict URL Path in Apache .htaccess