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
Also read:
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
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.
What if the subdomain is not under the domain’s document root but in its own directory? How do you set up wildcards to go up rather than down? For example:
ServerName http://www.example.com
DocumentRoot /var/www/example.com
and blog:
blog.example.com is at /var/www/blog.example.com
In such cases, you need to create a separate virtual host file for the subdomain and not include its configuration in the virtual host file of main domain. Basically treat it as another domain altogether.
I don’t know the details, but I know it is possible with the Apache module vhost_alias.
After a quick search, I found this post on Stack Overflow:
https://stackoverflow.com/questions/51633590/how-to-match-dynamic-sub-domain-alias-to-sub-folder-on-apache-server