Friday 20 September 2013

Apache: Set default virtual host for all new subdomains

It's so easy to auto config all new subdomain point to your vitual host.

NameVirtualHost *:80 

<VirtualHost *:80>
   ServerAdmin webmaster@defaultdomain.com
   DocumentRoot "/home/defaultdomain/www" 
   ServerName defaultdomain.com
   ServerAlias *.defaultdomain.com #apply for all subdomain#
   ErrorLog "/var/log/defaultdomain-error_log" 
   CustomLog "/var/log/defaultdomain-access_log" common
</VirtualHost>

So if you want to have a default website for all domain point to your server. How to do it ?

Apache use virtual host to run more than one website on a single Server. Each request will be match with respective website. If  the domain name is not configured in virtual host configuration file, Apache will run default website. That is the first virtual host configured so that to set a website is default for your server you have to put it on top of virtual host configuration file.


For example:

NameVirtualHost *:80 

#default website
<VirtualHost *:80>
   ServerAdmin webmaster@defaultdomain.com
   DocumentRoot "/home/defaultdomain/www" 
   ServerName defaultdomain.com
   ServerAlias www.defaultdomain.com
   ErrorLog "/var/log/defaultdomain-error_log" 
   CustomLog "/var/log/defaultdomain-access_log" common
</VirtualHost>

#other website
<VirtualHost *:80>
   ServerAdmin webmaster@otherdomain.com
   DocumentRoot "/home/otherdomain/www" 
   ServerName otherdomain.com
   ServerAlias www.otherdomain.com
   ErrorLog "/var/log/otherdomain-error_log" 
   CustomLog "/var/log/otherdomain-access_log" common
</VirtualHost>

0 nhận xét:

Post a Comment