This document attempts to answer the commonly-asked questions about setting up virtual hosts. These scenarios are those involving multiple web sites running on a single server, via name-based or IP-based virtual hosts. A document should be coming soon about running sites on several servers behind a single proxy server.
Your server has a single IP address, and multiple aliases (CNAMES)
    point to this machine in DNS. You want to run a web server for
    www.example1.com and www.example2.org on this
    machine.
Creating virtual
          host configurations on your Apache server does not magically
          cause DNS entries to be created for those host names. You
          must have the names in DNS, resolving to your IP
          address, or nobody else will be able to see your web site. You
          can put entries in your hosts file for local
          testing, but that will work only from the machine with those
          hosts entries.
The asterisks match all addresses, so the main server serves no
    requests. Due to the fact that www.example1.com is first
    in the configuration file, it has the highest priority and can be seen
    as the default or primary server. That means
    that if a request is received that does not match one of the specified
    ServerName directives, it will be served by this first
    VirtualHost.
You can, if you wish, replace * with the actual
            IP address of the system. In that case, the argument to
            VirtualHost must match the argument to
            NameVirtualHost:
However, it is additionally useful to use *
           on systems where the IP address is not predictable - for
           example if you have a dynamic IP address with your ISP, and
           you are using some variety of dynamic DNS solution. Since
           * matches any IP address, this configuration
           would work without changes whenever your IP address
           changes.
The above configuration is what you will want to use in almost all name-based virtual hosting situations. The only thing that this configuration will not work for, in fact, is when you are serving different content based on differing IP addresses or ports.
Any of the techniques discussed here can be extended to any number of IP addresses.
The server has two IP addresses. On one (172.20.30.40), we
    will serve the "main" server, server.domain.com and on the
    other (172.20.30.50), we will serve two or more virtual hosts.
Any request to an address other than 172.20.30.50 will be
    served from the main server. A request to 172.20.30.50 with an
    unknown hostname, or no Host: header, will be served from
    www.example1.com.
The server machine has two IP addresses (192.168.1.1
    and 172.20.30.40). The machine is sitting between an
    internal (intranet) network and an external (internet) network. Outside
    of the network, the name server.example.com resolves to
    the external address (172.20.30.40), but inside the
    network, that same name resolves to the internal address
    (192.168.1.1).
The server can be made to respond to internal and external requests
    with the same content, with just one VirtualHost
    section.
Now requests from both networks will be served from the same
    VirtualHost.
On the internal
          network, one can just use the name server rather
          than the fully qualified host name
          server.example.com.
Note also that, in the above example, you can replace the list
          of IP addresses with *, which will cause the server to
          respond the same on all addresses.
You have multiple domains going to the same IP and also want to serve multiple ports. By defining the ports in the "NameVirtualHost" tag, you can allow this to work. If you try using <VirtualHost name:port> without the NameVirtualHost name:port or you try to use the Listen directive, your configuration will not work.
The server has two IP addresses (172.20.30.40 and
    172.20.30.50) which resolve to the names
    www.example1.com and www.example2.org
    respectively.
Requests for any address not specified in one of the
    <VirtualHost> directives (such as
    localhost, for example) will go to the main server, if
    there is one.
The server machine has two IP addresses (172.20.30.40 and
    172.20.30.50) which resolve to the names
    www.example1.com and www.example2.org
    respectively. In each case, we want to run hosts on ports 80 and
    8080.
On some of my addresses, I want to do name-based virtual hosts, and on others, IP-based hosts.
_default_
    vhosts_default_ vhosts
    for all portsCatching every request to any unspecified IP address and port, i.e., an address/port combination that is not used for any other virtual host.
Using such a default vhost with a wildcard port effectively prevents any request going to the main server.
A default vhost never serves a request that was sent to an
    address/port that is used for name-based vhosts. If the request
    contained an unknown or no Host: header it is always
    served from the primary name-based vhost (the vhost for that
    address/port appearing first in the configuration file).
You can use 
_default_ vhosts
    for different portsSame as setup 1, but the server listens on several ports and we want
    to use a second _default_ vhost for port 80.
The default vhost for port 80 (which must appear before any default vhost with a wildcard port) catches all requests that were sent to an unspecified IP address. The main server is never used to serve a request.
_default_ vhosts
    for one portWe want to have a default vhost for port 80, but no other default vhosts.
A request to an unspecified address on port 80 is served from the default vhost any other request to an unspecified address and port is served from the main server.
The name-based vhost with the hostname
    www.example2.org (from our name-based example, setup 2) should get its own IP
    address. To avoid problems with name servers or proxies who cached the
    old IP address for the name-based vhost we want to provide both
    variants during a migration phase.
     The solution is easy, because we can simply add the new IP address
    (172.20.30.50) to the VirtualHost
    directive.
The vhost can now be accessed through the new address (as an IP-based vhost) and through the old address (as a name-based vhost).
ServerPath
	directiveWe have a server with two name-based vhosts. In order to match the
    correct virtual host a client must send the correct Host:
    header. Old HTTP/1.0 clients do not send such a header and Apache has
    no clue what vhost the client tried to reach (and serves the request
    from the primary vhost). To provide as much backward compatibility as
    possible we create a primary vhost which returns a single page
    containing links with an URL prefix to the name-based virtual
    hosts.
Due to the http://www.sub1.domain.tld/sub1/ is always served
    from the sub1-vhost.
 A request to the URL
    http://www.sub1.domain.tld/ is only
    served from the sub1-vhost if the client sent a correct
    Host: header. If no Host: header is sent the
    client gets the information page from the primary host.
     Please note that there is one oddity: A request to
    http://www.sub2.domain.tld/sub1/ is also served from the
    sub1-vhost if the client sent no Host: header.
     The Host: header can use both URL variants, i.e.,
    with or without URL prefix.