Use Top-Level-Domain (TLD) Wildcard in Apache Virtual Host

Written by - 0 comments

Published on October 10th 2013 - Listed in Internet Linux Unix


An Apache (2.2) virtual host can use wildcards for subdomains, as it is described on the official Apache documentation for Serveralias:

 The ServerAlias directive sets the alternate names for a host, for use with name-based virtual hosts. The ServerAlias may include wildcards, if appropriate.

In the example on the same page, the following VirtualHost section is shown:

<VirtualHost *:80>
ServerName server.domain.com
ServerAlias server server2.domain.com server2
ServerAlias *.example.com
UseCanonicalName Off
# ...
</VirtualHost>

In this example a wildcard for all subdomains of example.com (*.example.com) is used.

But what if someone wants to use wildcards not only for a domain, but for a whole TLD, let's say for *.com?
I was surprised... it works!

For a testing scenario, I created two vhosts:

<VirtualHost *:80>
  ServerName ch.example.com
  ServerAlias *.ch
  DocumentRoot /home/www/ch
[...]
</VirtualHost>

<VirtualHost *:80>
  ServerName fr.example.com
  ServerAlias *.fr
  DocumentRoot /home/www/fr
[...]
</VirtualHost>

When I accessed "www.bla.ch" (just some fantasy domain name with the .ch tld and pointing to the web server's ip address), the correct index page was shown. The same happened, when I access www.bla.fr.

Setups like these can be helpful on a dedicated Apache web server for a multinational company, where each country has its own website.


Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.