<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
<!-- $Revision: 1.5.2.9 $ -->

<!--
 Copyright 2002-2004 The Apache Software Foundation

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<manualpage metafile="name-based.xml.meta">
<parentdocument href="./">Virtual Hosts</parentdocument>
<title>Name-based Virtual Host Support</title>

<summary>
    <p>This document describes when and how to use name-based virtual hosts.</p>
</summary>

<seealso><a href="ip-based.html">IP-based Virtual Host Support</a></seealso>
<seealso><a href="details.html">An In-Depth Discussion of Virtual Host Matching</a></seealso>
<seealso><a href="mass.html">Dynamically configured mass virtual hosting</a></seealso>
<seealso><a href="examples.html">Virtual Host examples for common setups</a></seealso>
<seealso><a href="examples.html#serverpath">ServerPath configuration example</a></seealso>

<section id="namevip"><title>Name-based vs. IP-based Virtual Hosts</title>

    <p>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.</p>

    <p>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 there is a specific reason to
    choose IP-based virtual hosting.  Some reasons why you might consider
    using IP-based virtual hosting:</p>

    <ul>
        <li>Some ancient clients are not compatible with name-based virtual
        hosting.  For name-based virtual hosting to work, the client must send
        the HTTP Host header.  This is required by HTTP/1.1, and is
        implemented by all modern HTTP/1.0 browsers as an extension.  If you
        need to support obsolete clients and still use name-based virtual
        hosting, a possible technique is discussed at the end of this
        document.</li>

        <li>Name-based virtual hosting cannot be used with SSL secure servers
        because of the nature of the SSL protocol.</li>

        <li>Some operating systems and network equipment implement bandwidth
        management techniques that cannot differentiate between hosts unless
        they are on separate IP addresses.</li>
    </ul>

</section>

<section id="using"><title>Using Name-based Virtual Hosts</title>

<related>
    <modulelist>
    <module>core</module>
    </modulelist>

    <directivelist>
	<directive module="core">DocumentRoot</directive>
	<directive module="core">NameVirtualHost</directive>
	<directive module="core">ServerAlias</directive>
	<directive module="core">ServerName</directive>
	<directive module="core">ServerPath</directive>
	<directive module="core" type="section">VirtualHost</directive>
    </directivelist>
</related>

    <p>To use name-based virtual hosting, you must designate the IP
    address (and possibly port) on the server that will be accepting
    requests for the hosts.  This is configured using the <directive
    module="core">NameVirtualHost</directive> directive.
    In the normal case where any and all IP addresses on the server should
    be used, you can use <code>*</code> as the argument to <directive
    module="core">NameVirtualHost</directive>. If you're planning to use
    multiple ports (e.g. running SSL) you should add a Port to the argument,
    such as <code>*:80</code>. Note that mentioning an IP address in a
    <directive module="core">NameVirtualHost</directive> directive does not
    automatically make the server listen to that IP address. See
    <a href="../bind.html">Setting which addresses and ports Apache uses</a>
    for more details. In addition, any IP address specified here must be
    associated with a network interface	on the server.</p>

    <p>The next step is to create a <directive type="section"
    module="core">VirtualHost</directive> block for
    each different host that you would like to serve. The argument to the
    <directive type="section" module="core">VirtualHost</directive> directive
    should be the same as the argument to the <directive
    module="core">NameVirtualHost</directive> directive (ie, an IP address,
    or <code>*</code> for all addresses).  Inside each <directive type="section"	
    module="core">VirtualHost</directive> block, you will need at minimum a
    <directive module="core">ServerName</directive> directive to designate
    which host is served and a <directive module="core">DocumentRoot</directive>
    directive to show where in the filesystem the content for that host
    lives.</p>

    <note><title>Main host goes away</title>
        <p>If you are adding virtual hosts to an existing web server, you
        must also create a <directive type="section" module="core"
        >VirtualHost</directive> block for the existing host. The <directive
        module="core">ServerName</directive> and <directive module="core"
        >DocumentRoot</directive> included in this virtual host should be the
        same as the global <directive module="core">ServerName</directive> and
        <directive module="core">DocumentRoot</directive>. List this virtual
        host first in the configuration file so that it will act as the default
        host.</p>
    </note>

    <p>For example, suppose that you are serving the domain
    <code>www.domain.tld</code> and you wish to add the virtual host
    <code>www.otherdomain.tld</code>, which points at the same IP address.
    Then you simply add the following to <code>httpd.conf</code>:</p>

    <example>
        NameVirtualHost *:80<br />
        <br />
        &lt;VirtualHost *:80&gt;<br />
        <indent>
            ServerName www.domain.tld<br />
            ServerAlias domain.tld *.domain.tld<br />
            DocumentRoot /www/domain<br />
        </indent>
        &lt;/VirtualHost&gt;<br />
        <br />
        &lt;VirtualHost *:80&gt;<br />
        <indent>ServerName www.otherdomain.tld<br />
            DocumentRoot /www/otherdomain<br />
        </indent>
        &lt;/VirtualHost&gt;<br />
    </example>

    <p>You can alternatively specify an explicit IP address in place of the
    <code>*</code> in both the <directive module="core"
    >NameVirtualHost</directive> and <directive type="section" module="core"
    >VirtualHost</directive> directives. For example, you might want to do this
    in order to run some name-based virtual hosts on one IP address, and either
    IP-based, or another set of name-based virtual hosts on another address.</p>

    <p>Many servers want to be accessible by more than one name. This is
    possible with the <directive module="core">ServerAlias</directive>
    directive, placed inside the <directive type="section" module="core"
    >VirtualHost</directive> section. For example in the first <directive
    type="section" module="core">VirtualHost</directive> block above, the
    <directive module="core">ServerAlias</directive> directive indicates that
    the listed names are other names which people can use to see that same
    web site:</p>

    <example>
        ServerAlias domain.tld *.domain.tld
    </example>

    <p>then requests for all hosts in the <code>domain.tld</code> domain will
    be served by the <code>www.domain.tld</code> virtual host. The wildcard
    characters <code>*</code> and <code>?</code> can be used to match names.
    Of course, you can't just make up names and place them in <directive
    module="core">ServerName</directive> or <code>ServerAlias</code>. You must
    first have your DNS server properly configured to map those names to an IP
    address associated with your server.</p>

    <p>Finally, you can fine-tune the configuration of the virtual hosts
    by placing other directives inside the <directive type="section"
    module="core">VirtualHost</directive> containers. Most directives can be
    placed in these containers and will then change the configuration only of
    the relevant virtual host. To find out if a particular directive is allowed,
    check the <a href="../mod/directive-dict.html#Context">Context</a> of the
    directive. Configuration directives set in the <em>main server context</em>
    (outside any <directive type="section" module="core">VirtualHost</directive>
    container) will be used only if they are not overridden by the virtual host
    settings.</p>

    <p>Now when a request arrives, the server will first check if it is using
    an IP address that matches the <directive module="core"
    >NameVirtualHost</directive>. If it is, then it will look at each <directive
    type="section" module="core">VirtualHost</directive> section with a matching
    IP address and try to find one where the <directive module="core"
    >ServerName</directive> or <code>ServerAlias</code> matches the requested
    hostname. If it finds one, then it uses the configuration for that server.
    If no matching virtual host  is found, then <strong>the first listed virtual
    host</strong> that matches the IP address will be used.</p>

    <p>As a consequence, the first listed virtual host is the <em>default</em>
    virtual host.  The <directive module="core">DocumentRoot</directive> from
    the <em>main server</em> will <strong>never</strong> be used when an IP
    address matches the <directive module="core">NameVirtualHost</directive> 
    directive. If you would like to have a special configuration for requests 
    that do not	match any particular virtual host, simply put that configuration
    in a <directive type="section" module="core">VirtualHost</directive>
    container and list it first in the configuration file.</p>

</section>

<section id="compat"><title>Compatibility with Older Browsers</title>

    <p>As mentioned earlier, there are some clients
    who do not send the required data for the name-based virtual
    hosts to work properly. These clients will always be sent the
    pages from the first virtual host listed for that IP address
    (the <cite>primary</cite> name-based virtual host).</p>

    <note><title>How much older?</title>
    <p>Please note that when we say older, we really do mean older. You are
    very unlikely to encounter one of these browsers in use today. All
    current versions of any browser send the <code>Host</code> header as
    required for name-based virtual hosts.</p>
    </note>

    <p>There is a possible workaround with the <directive
    module="core">ServerPath</directive>
    directive, albeit a slightly cumbersome one:</p>

    <p>Example configuration:</p>

    <example>
        NameVirtualHost 111.22.33.44<br />
        <br />
        &lt;VirtualHost 111.22.33.44&gt;<br />
        <indent>
            ServerName www.domain.tld<br />
            ServerPath /domain<br />
            DocumentRoot /web/domain<br />
        </indent>
        &lt;/VirtualHost&gt;<br />
    </example>

    <p>What does this mean? It means that a request for any URI
    beginning with "<code>/domain</code>" will be served from the
    virtual host <code>www.domain.tld</code>. This means that the
    pages can be accessed as <code>http://www.domain.tld/domain/</code> 
    for all clients, although clients sending a <code>Host:</code> header 
    can also access it as <code>http://www.domain.tld/</code>.</p>

    <p>In order to make this work, put a link on your primary
    virtual host's page to
    <code>http://www.domain.tld/domain/</code>. Then, in the virtual
    host's pages, be sure to use either purely relative links
    (<em>e.g.</em>, "<code>file.html</code>" or
    "<code>../icons/image.gif</code>") or links containing the
    prefacing <code>/domain/</code> (<em>e.g.</em>,
    "<code>http://www.domain.tld/domain/misc/file.html</code>" or
    "<code>/domain/misc/file.html</code>").</p>

    <p>This requires a bit of discipline, but adherence to these
    guidelines will, for the most part, ensure that your pages will
    work with all browsers, new and old.</p>

</section>
</manualpage>
