NameVirtualHost * <VirtualHost *> ServerName example.com ServerAlias www.example.com ServerAdmin firstname.lastname@example.org DocumentRoot /var/www/virtual/example.com CustomLog /var/log/apache/example.com-access.log combined ErrorLog /var/log/apache/example.com-error.log </VirtualHost> <VirtualHost *> ServerName example.net ServerAlias www.example.net ServerAdmin email@example.com DocumentRoot /var/www/virtual/example.net CustomLog /var/log/apache/example.net-access.log combined ErrorLog /var/log/apache/example.net-error.log </VirtualHost>
If the server has more than one IP address and you want it to serve a particular site only one a specific one of these addresses, you can replace the asterisks in that configuration with a particular address and optionally a port, eg.
NameVirtualHost 184.108.40.206:80 <VirtualHost 220.127.116.11:80> ServerName example.com ...
The arguments of the NameVirtualHost and corresponding VirtualHost directives must match exactly. Of course, if your server has several IPs you probably want several NameVirtualHost directives too – one for every IP. You can then serve a different set of sites from each IP.
Note that you can also use things like “*:80” to serve a particular site only on a particular port, but on every IP of the server.
If you have a site that can be reached through several domains/hostnames, eg. with or without the www. and either .com or .co.nz, you might want to consider making one of them the canonical address. This is better for any number of reasons: it will make your referrer logs easier to process; it will make things more obvious for search engines (might improve your ranking!); people will use almost exclusively the canonical address to link to your site, making it easier to find out who is linking to you; and on and on. With Apache virtual hosting, you can do this easily; in the following example, www.example.co.nz was chosen as the canonical address:
NameVirtualHost * <VirtualHost *> ServerName www.example.co.nz ServerAdmin firstname.lastname@example.org DocumentRoot /var/www/virtual/example.co.nz CustomLog /var/log/apache/example.co.nz-access.log combined ErrorLog /var/log/apache/example.co.nz-error.log </VirtualHost> <VirtualHost *> ServerName aliases.for.www.example.co.nz ServerAlias example.co.nz ServerAlias example.com ServerAlias www.example.com RedirectMatch 301 /?(.*) http://www.example.co.nz/$1 </VirtualHost>
With this configuration, any and every request for a page on one of the alias domains, eg. http://example.com/products/, will redirect the visitor the same page in the canonical domain, in this case http://www.example.co.nz/products/.
There are other ways to achieve this, such as using mod_rewrite, but none of them is as simple as this one.
The advantage with using the aliases.for.www.example.co.nz as the ServerName in the second VirtualHost is that when using the command apache2ctl -S you get the following output:
port 80 namevhost www.example.co.nz (/etc/apache2/sites-enabled/ubersite.conf:3) port 80 namevhost aliases.for.www.example.co.nz (/etc/apache2/sites-enabled/ubersite.conf:11)