I have various differentes subdomains pointing to same directory/application in my apache 2.2.
Like this:
<VirtualHost *:80>
ServerName subdomain1.domain.com.br
ServerAdmin
DocumentRoot /var/www/application
</VirtualHost>
<VirtualHost *:80>
ServerName subdomain2.domain.com.br
ServerAdmin
DocumentRoot /var/www/application
</VirtualHost>The problem is to configure apache every subdomain is added in DNS Server.
Its possible to have only one virtualhost, representing all subdomains ?
Something like this:
<VirtualHost *:80>
ServerName *.domain.com.br
ServerAdmin
DocumentRoot /var/www/application
</VirtualHost> 1 Answer
Yes it is possible, but in the article VirtualHost Examples is no mentioned syntax like ServerName *.domain.com.br. In this case you have to use ServerAlias directive, and the VirtualHost configuration should be something like this:
<VirtualHost *:80> ServerName domain.com.br ServerAlias *.domain.com.br ServerAdmin DocumentRoot /var/www/application
</VirtualHost>Please note that the directive ServerAlias can be used only in the virtual host context.
I think in the case when all of your sub-domains points to one DocumentRoot folder, if you set ServerName in server context the result will be almost the same:
<VirtualHost *:80>ServerName domain.com.brServerAlias *.domain.com.brServerAdmin DocumentRoot /var/www/application</VirtualHost>
In this case you must enter in /etc/hosts following:
xxx.xxx.xxx.xxx domain.com.brWhere xxx.xxx.xxx.xxx is your server's IP address.