This document describes when and how to use name-based virtual hosts.
IP-based virtual hosts use the IP address of the connection to determine the correct virtual host to serve. Therefore you need to have a separate IP address for each host.
With name-based virtual hosting, the server relies on the client to report the hostname as part of the HTTP headers. Using this technique, many different hosts can share the same IP address.
Name-based virtual hosting is usually simpler, since you need only configure your DNS server to map each hostname to the correct IP address and then configure the Apache HTTP Server to recognize the different hostnames. Name-based virtual hosting also eases the demand for scarce IP addresses. Therefore you should use name-based virtual hosting unless you are using equipment that explicitly demands IP-based hosting. Historical reasons for IP-based virtual hosting based on client support are no longer applicable to a general-purpose web server.
Name-based virtual hosting builds off of the IP-based virtual host selection algorithm, meaning that searches for the proper server name occur only between virtual hosts that have the best IP-based address.
It is important to recognize that the first step in name-based virtual host resolution is IP-based resolution. Name-based virtual host resolution only chooses the most appropriate name-based virtual host after narrowing down the candidates to the best IP-based match. Using a wildcard (*) for the IP address in all of the VirtualHost directives makes this IP-based mapping irrelevant.
When a request arrives, the server will find the best (most specific) matching
If no matching ServerName or ServerAlias is found in the set of virtual hosts containing the most specific matching IP address and port combination, then the first listed virtual host that matches that will be used.
The first step is to create a
Any request that doesn't match an existing
When you add a name-based virtual host to an existing server, and
the virtual host arguments match preexisting IP and port combinations,
requests will now be handled by an explicit virtual host. In this case,
it's usually wise to create a default virtual host
with a
For example, suppose that you are serving the domain
www.example.com
and you wish to add the virtual host
other.example.com
, which points at the same IP address.
Then you simply add the following to httpd.conf
:
You can alternatively specify an explicit IP address in place of the
*
in
Many servers want to be accessible by more than one name. This is
possible with the
then requests for all hosts in the example.com
domain will
be served by the www.example.com
virtual host. The wildcard
characters *
and ?
can be used to match names.
Of course, you can't just make up names and place them in ServerAlias
. You must
first have your DNS server properly configured to map those names to an IP
address associated with your server.
Finally, you can fine-tune the configuration of the virtual hosts
by placing other directives inside the