home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / apache_2.2.8-win32-x86-no_ssl.msi / Data1.cab / _7056BE3D3D17FF7E392645917607A744 < prev    next >
Extensible Markup Language  |  2007-09-01  |  27KB  |  660 lines

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
  4.         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  5.               This file is generated from xml source: DO NOT EDIT
  6.         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  7.       -->
  8. <title>VirtualHost Examples - Apache HTTP Server</title>
  9. <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
  10. <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
  11. <link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
  12. <link href="../images/favicon.ico" rel="shortcut icon" /></head>
  13. <body id="manual-page"><div id="page-header">
  14. <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
  15. <p class="apache">Apache HTTP Server Version 2.2</p>
  16. <img alt="" src="../images/feather.gif" /></div>
  17. <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
  18. <div id="path">
  19. <a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.2</a> > <a href="./">Virtual Hosts</a></div><div id="page-content"><div id="preamble"><h1>VirtualHost Examples</h1>
  20. <div class="toplang">
  21. <p><span>Available Languages: </span><a href="../en/vhosts/examples.html" title="English"> en </a> |
  22. <a href="../fr/vhosts/examples.html" hreflang="fr" rel="alternate" title="Franτais"> fr </a> |
  23. <a href="../ja/vhosts/examples.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |
  24. <a href="../ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
  25. </div>
  26.  
  27.  
  28.     <p>This document attempts to answer the commonly-asked questions about
  29.     setting up virtual hosts. These scenarios are those involving multiple
  30.     web sites running on a single server, via <a href="name-based.html">name-based</a> or <a href="ip-based.html">IP-based</a> virtual hosts.
  31.     </p>
  32.  
  33. </div>
  34. <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#purename">Running several name-based web
  35.     sites on a single IP address.</a></li>
  36. <li><img alt="" src="../images/down.gif" /> <a href="#twoips">Name-based hosts on more than one
  37.     IP address.</a></li>
  38. <li><img alt="" src="../images/down.gif" /> <a href="#intraextra">Serving the same content on
  39.     different IP addresses (such as an internal and external
  40.     address).</a></li>
  41. <li><img alt="" src="../images/down.gif" /> <a href="#port">Running different sites on different
  42.     ports.</a></li>
  43. <li><img alt="" src="../images/down.gif" /> <a href="#ip">IP-based virtual hosting</a></li>
  44. <li><img alt="" src="../images/down.gif" /> <a href="#ipport">Mixed port-based and ip-based virtual
  45.     hosts</a></li>
  46. <li><img alt="" src="../images/down.gif" /> <a href="#mixed">Mixed name-based and IP-based
  47.     vhosts</a></li>
  48. <li><img alt="" src="../images/down.gif" /> <a href="#proxy">Using <code>Virtual_host</code> and
  49.     mod_proxy together</a></li>
  50. <li><img alt="" src="../images/down.gif" /> <a href="#default">Using <code>_default_</code>
  51.     vhosts</a></li>
  52. <li><img alt="" src="../images/down.gif" /> <a href="#migrate">Migrating a name-based vhost to an
  53.     IP-based vhost</a></li>
  54. <li><img alt="" src="../images/down.gif" /> <a href="#serverpath">Using the <code>ServerPath</code>
  55.     directive</a></li>
  56. </ul></div>
  57. <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  58. <div class="section">
  59. <h2><a name="purename" id="purename">Running several name-based web
  60.     sites on a single IP address.</a></h2>
  61.  
  62.     <p>Your server has a single IP address, and multiple aliases (CNAMES)
  63.     point to this machine in DNS. You want to run a web server for
  64.     <code>www.example.com</code> and <code>www.example.org</code> on this
  65.     machine.</p>
  66.  
  67.     <div class="note"><h3>Note</h3><p>Creating virtual
  68.           host configurations on your Apache server does not magically
  69.           cause DNS entries to be created for those host names. You
  70.           <em>must</em> have the names in DNS, resolving to your IP
  71.           address, or nobody else will be able to see your web site. You
  72.           can put entries in your <code>hosts</code> file for local
  73.           testing, but that will work only from the machine with those
  74.           hosts entries.</p>
  75.     </div>
  76.  
  77.     <div class="example"><h3>Server configuration</h3><p><code>
  78.     
  79.  
  80.     # Ensure that Apache listens on port 80<br />
  81.     Listen 80<br />
  82.     <br />
  83.     # Listen for virtual host requests on all IP addresses<br />
  84.     NameVirtualHost *:80<br />
  85.     <br />
  86.     <VirtualHost *:80><br />
  87.     <span class="indent">
  88.       DocumentRoot /www/example1<br />
  89.       ServerName www.example.com<br />
  90.       <br />
  91.       # Other directives here<br />
  92.       <br />
  93.     </span>
  94.     </VirtualHost><br />
  95.     <br />
  96.     <VirtualHost *:80><br />
  97.     <span class="indent">
  98.       DocumentRoot /www/example2<br />
  99.       ServerName www.example.org<br />
  100.       <br />
  101.       # Other directives here<br />
  102.       <br />
  103.     </span>
  104.     </VirtualHost>
  105.     </code></p></div>
  106.  
  107.     <p>The asterisks match all addresses, so the main server serves no
  108.     requests. Due to the fact that <code>www.example.com</code> is first
  109.     in the configuration file, it has the highest priority and can be seen
  110.     as the <cite>default</cite> or <cite>primary</cite> server. That means
  111.     that if a request is received that does not match one of the specified
  112.     <code>ServerName</code> directives, it will be served by this first
  113.     <code>VirtualHost</code>.</p>
  114.  
  115.     <div class="note">
  116.             <h3>Note</h3>
  117.  
  118.             <p>You can, if you wish, replace <code>*</code> with the actual
  119.             IP address of the system. In that case, the argument to
  120.             <code>VirtualHost</code> <em>must</em> match the argument to
  121.             <code>NameVirtualHost</code>:</p>
  122.  
  123.             <div class="example"><p><code>
  124.             NameVirtualHost 172.20.30.40<br />
  125.                         <br />
  126.             <VirtualHost 172.20.30.40><br />
  127.                  # etc ...
  128.             </code></p></div>
  129.  
  130.            <p>However, it is additionally useful to use <code>*</code>
  131.            on systems where the IP address is not predictable - for
  132.            example if you have a dynamic IP address with your ISP, and
  133.            you are using some variety of dynamic DNS solution. Since
  134.            <code>*</code> matches any IP address, this configuration
  135.            would work without changes whenever your IP address
  136.            changes.</p>
  137.     </div>
  138.  
  139.     <p>The above configuration is what you will want to use in almost
  140.     all name-based virtual hosting situations. The only thing that this
  141.     configuration will not work for, in fact, is when you are serving
  142.     different content based on differing IP addresses or ports.</p>
  143.  
  144.     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  145. <div class="section">
  146. <h2><a name="twoips" id="twoips">Name-based hosts on more than one
  147.     IP address.</a></h2>
  148.  
  149.       <div class="note">
  150.           <h3>Note</h3><p>Any of the
  151.           techniques discussed here can be extended to any number of IP
  152.           addresses.</p>
  153.     </div>
  154.  
  155.     <p>The server has two IP addresses. On one (<code>172.20.30.40</code>), we
  156.     will serve the "main" server, <code>server.domain.com</code> and on the
  157.     other (<code>172.20.30.50</code>), we will serve two or more virtual hosts.</p>
  158.  
  159.     <div class="example"><h3>Server configuration</h3><p><code>
  160.     
  161.  
  162.     Listen 80<br />
  163.         <br />
  164.     # This is the "main" server running on 172.20.30.40<br />
  165.     ServerName server.domain.com<br />
  166.     DocumentRoot /www/mainserver<br />
  167.         <br />
  168.     # This is the other address<br />
  169.     NameVirtualHost 172.20.30.50<br />
  170.         <br />
  171.     <VirtualHost 172.20.30.50><br />
  172.     <span class="indent">
  173.         DocumentRoot /www/example1<br />
  174.         ServerName www.example.com<br />
  175.                <br />
  176.         # Other directives here ...<br />
  177.                 <br />
  178.     </span>
  179.     </VirtualHost><br />
  180.         <br />
  181.     <VirtualHost 172.20.30.50><br />
  182.     <span class="indent">
  183.         DocumentRoot /www/example2<br />
  184.         ServerName www.example.org<br />
  185.                 <br />
  186.         # Other directives here ...<br />
  187.                 <br />
  188.     </span>
  189.     </VirtualHost>
  190.     </code></p></div>
  191.  
  192.     <p>Any request to an address other than <code>172.20.30.50</code> will be
  193.     served from the main server. A request to <code>172.20.30.50</code> with an
  194.     unknown hostname, or no <code>Host:</code> header, will be served from
  195.     <code>www.example.com</code>.</p>
  196.  
  197.     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  198. <div class="section">
  199. <h2><a name="intraextra" id="intraextra">Serving the same content on
  200.     different IP addresses (such as an internal and external
  201.     address).</a></h2>
  202.  
  203.     <p>The server machine has two IP addresses (<code>192.168.1.1</code>
  204.     and <code>172.20.30.40</code>). The machine is sitting between an
  205.     internal (intranet) network and an external (internet) network. Outside
  206.     of the network, the name <code>server.example.com</code> resolves to
  207.     the external address (<code>172.20.30.40</code>), but inside the
  208.     network, that same name resolves to the internal address
  209.     (<code>192.168.1.1</code>).</p>
  210.  
  211.     <p>The server can be made to respond to internal and external requests
  212.     with the same content, with just one <code>VirtualHost</code>
  213.     section.</p>
  214.  
  215.     <div class="example"><h3>Server configuration</h3><p><code>
  216.     
  217.  
  218.     NameVirtualHost 192.168.1.1<br />
  219.     NameVirtualHost 172.20.30.40<br />
  220.         <br />
  221.     <VirtualHost 192.168.1.1 172.20.30.40><br />
  222.     <span class="indent">
  223.         DocumentRoot /www/server1<br />
  224.         ServerName server.example.com<br />
  225.         ServerAlias server<br />
  226.     </span>
  227.     </VirtualHost>
  228.     </code></p></div>
  229.  
  230.     <p>Now requests from both networks will be served from the same
  231.     <code>VirtualHost</code>.</p>
  232.  
  233.     <div class="note">
  234.           <h3>Note:</h3><p>On the internal
  235.           network, one can just use the name <code>server</code> rather
  236.           than the fully qualified host name
  237.           <code>server.example.com</code>.</p>
  238.  
  239.           <p>Note also that, in the above example, you can replace the list
  240.           of IP addresses with <code>*</code>, which will cause the server to
  241.           respond the same on all addresses.</p>
  242.     </div>
  243.  
  244.     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  245. <div class="section">
  246. <h2><a name="port" id="port">Running different sites on different
  247.     ports.</a></h2>
  248.  
  249.     <p>You have multiple domains going to the same IP and also want to
  250.     serve multiple ports. By defining the ports in the "NameVirtualHost"
  251.     tag, you can allow this to work. If you try using <VirtualHost
  252.     name:port> without the NameVirtualHost name:port or you try to use
  253.     the Listen directive, your configuration will not work.</p>
  254.  
  255.     <div class="example"><h3>Server configuration</h3><p><code>
  256.     
  257.  
  258.     Listen 80<br />
  259.     Listen 8080<br />
  260.         <br />
  261.     NameVirtualHost 172.20.30.40:80<br />
  262.     NameVirtualHost 172.20.30.40:8080<br />
  263.         <br />
  264.     <VirtualHost 172.20.30.40:80><br />
  265.     <span class="indent">
  266.         ServerName www.example.com<br />
  267.         DocumentRoot /www/domain-80<br />
  268.     </span>
  269.     </VirtualHost><br />
  270.         <br />
  271.     <VirtualHost 172.20.30.40:8080><br />
  272.     <span class="indent">
  273.         ServerName www.example.com<br />
  274.         DocumentRoot /www/domain-8080<br />
  275.     </span>
  276.     </VirtualHost><br />
  277.         <br />
  278.     <VirtualHost 172.20.30.40:80><br />
  279.     <span class="indent">
  280.         ServerName www.example.org<br />
  281.         DocumentRoot /www/otherdomain-80<br />
  282.     </span>
  283.     </VirtualHost><br />
  284.         <br />
  285.     <VirtualHost 172.20.30.40:8080><br />
  286.     <span class="indent">
  287.         ServerName www.example.org<br />
  288.         DocumentRoot /www/otherdomain-8080<br />
  289.     </span>
  290.     </VirtualHost>
  291.     </code></p></div>
  292.  
  293.     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  294. <div class="section">
  295. <h2><a name="ip" id="ip">IP-based virtual hosting</a></h2>
  296.  
  297.     <p>The server has two IP addresses (<code>172.20.30.40</code> and
  298.     <code>172.20.30.50</code>) which resolve to the names
  299.     <code>www.example.com</code> and <code>www.example.org</code>
  300.     respectively.</p>
  301.  
  302.     <div class="example"><h3>Server configuration</h3><p><code>
  303.     
  304.  
  305.     Listen 80<br />
  306.         <br />
  307.     <VirtualHost 172.20.30.40><br />
  308.     <span class="indent">
  309.         DocumentRoot /www/example1<br />
  310.         ServerName www.example.com<br />
  311.     </span>
  312.     </VirtualHost><br />
  313.         <br />
  314.     <VirtualHost 172.20.30.50><br />
  315.     <span class="indent">
  316.         DocumentRoot /www/example2<br />
  317.         ServerName www.example.org<br />
  318.     </span>
  319.     </VirtualHost>
  320.     </code></p></div>
  321.  
  322.     <p>Requests for any address not specified in one of the
  323.     <code><VirtualHost></code> directives (such as
  324.     <code>localhost</code>, for example) will go to the main server, if
  325.     there is one.</p>
  326.  
  327.     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  328. <div class="section">
  329. <h2><a name="ipport" id="ipport">Mixed port-based and ip-based virtual
  330.     hosts</a></h2>
  331.  
  332.     <p>The server machine has two IP addresses (<code>172.20.30.40</code> and
  333.     <code>172.20.30.50</code>) which resolve to the names
  334.     <code>www.example.com</code> and <code>www.example.org</code>
  335.     respectively. In each case, we want to run hosts on ports 80 and
  336.     8080.</p>
  337.  
  338.     <div class="example"><h3>Server configuration</h3><p><code>
  339.     
  340.  
  341.     Listen 172.20.30.40:80<br />
  342.     Listen 172.20.30.40:8080<br />
  343.     Listen 172.20.30.50:80<br />
  344.     Listen 172.20.30.50:8080<br />
  345.         <br />
  346.     <VirtualHost 172.20.30.40:80><br />
  347.     <span class="indent">
  348.         DocumentRoot /www/example1-80<br />
  349.         ServerName www.example.com<br />
  350.     </span>
  351.     </VirtualHost><br />
  352.         <br />
  353.     <VirtualHost 172.20.30.40:8080><br />
  354.     <span class="indent">
  355.         DocumentRoot /www/example1-8080<br />
  356.         ServerName www.example.com<br />
  357.         </span>
  358.     </VirtualHost><br />
  359.         <br />
  360.     <VirtualHost 172.20.30.50:80><br />
  361.     <span class="indent">
  362.         DocumentRoot /www/example2-80<br />
  363.         ServerName www.example.org<br />
  364.     </span>
  365.     </VirtualHost><br />
  366.         <br />
  367.     <VirtualHost 172.20.30.50:8080><br />
  368.     <span class="indent">
  369.         DocumentRoot /www/example2-8080<br />
  370.         ServerName www.example.org<br />
  371.     </span>
  372.     </VirtualHost>
  373.     </code></p></div>
  374.  
  375.     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  376. <div class="section">
  377. <h2><a name="mixed" id="mixed">Mixed name-based and IP-based
  378.     vhosts</a></h2>
  379.  
  380.     <p>On some of my addresses, I want to do name-based virtual hosts, and
  381.     on others, IP-based hosts.</p>
  382.  
  383.     <div class="example"><h3>Server configuration</h3><p><code>
  384.     
  385.  
  386.     Listen 80<br />
  387.         <br />
  388.     NameVirtualHost 172.20.30.40<br />
  389.         <br />
  390.     <VirtualHost 172.20.30.40><br />
  391.     <span class="indent">
  392.         DocumentRoot /www/example1<br />
  393.         ServerName www.example.com<br />
  394.     </span>
  395.     </VirtualHost><br />
  396.         <br />
  397.     <VirtualHost 172.20.30.40><br />
  398.     <span class="indent">
  399.         DocumentRoot /www/example2<br />
  400.         ServerName www.example.org<br />
  401.     </span>
  402.     </VirtualHost><br />
  403.         <br />
  404.     <VirtualHost 172.20.30.40><br />
  405.     <span class="indent">
  406.         DocumentRoot /www/example3<br />
  407.         ServerName www.example3.net<br />
  408.     </span>
  409.     </VirtualHost><br />
  410.         <br />
  411.     # IP-based<br />
  412.     <VirtualHost 172.20.30.50><br />
  413.     <span class="indent">
  414.         DocumentRoot /www/example4<br />
  415.         ServerName www.example4.edu<br />
  416.     </span>
  417.     </VirtualHost><br />
  418.         <br />
  419.     <VirtualHost 172.20.30.60><br />
  420.     <span class="indent">
  421.         DocumentRoot /www/example5<br />
  422.         ServerName www.example5.gov<br />
  423.     </span>
  424.     </VirtualHost>
  425.     </code></p></div>
  426.  
  427.     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  428. <div class="section">
  429. <h2><a name="proxy" id="proxy">Using <code>Virtual_host</code> and
  430.     mod_proxy together</a></h2>
  431.  
  432.     <p>The following example allows a front-end machine to proxy a
  433.     virtual host through to a server running on another machine. In the
  434.     example, a virtual host of the same name is configured on a machine
  435.     at <code>192.168.111.2</code>. The <code class="directive"><a href="../mod/mod_proxy.html#proxypreservehost on">ProxyPreserveHost On</a></code> directive is
  436.     used so that the desired hostname is passed through, in case we are
  437.     proxying multiple hostnames to a single machine.</p>
  438.  
  439.     <div class="example"><p><code>
  440.     <VirtualHost *:*><br />
  441.         ProxyPreserveHost On<br />
  442.         ProxyPass / http://192.168.111.2<br />
  443.         ProxyPassReverse / http://192.168.111.2/<br />
  444.         ServerName hostname.example.com<br />
  445.     </VirtualHost>
  446.     </code></p></div>
  447.  
  448.     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  449. <div class="section">
  450. <h2><a name="default" id="default">Using <code>_default_</code>
  451.     vhosts</a></h2>
  452.  
  453.       <h3><a name="defaultallports" id="defaultallports"><code>_default_</code> vhosts
  454.     for all ports</a></h3>
  455.  
  456.     <p>Catching <em>every</em> request to any unspecified IP address and
  457.     port, <em>i.e.</em>, an address/port combination that is not used for
  458.     any other virtual host.</p>
  459.  
  460.     <div class="example"><h3>Server configuration</h3><p><code>
  461.     
  462.  
  463.     <VirtualHost _default_:*><br />
  464.     <span class="indent">
  465.         DocumentRoot /www/default<br />
  466.     </span>
  467.     </VirtualHost>
  468.     </code></p></div>
  469.  
  470.     <p>Using such a default vhost with a wildcard port effectively prevents
  471.     any request going to the main server.</p>
  472.  
  473.     <p>A default vhost never serves a request that was sent to an
  474.     address/port that is used for name-based vhosts. If the request
  475.     contained an unknown or no <code>Host:</code> header it is always
  476.     served from the primary name-based vhost (the vhost for that
  477.     address/port appearing first in the configuration file).</p>
  478.  
  479.     <p>You can use <code class="directive"><a href="../mod/mod_alias.html#aliasmatch">AliasMatch</a></code> or
  480.     <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> to rewrite any
  481.     request to a single information page (or script).</p>
  482.     
  483.  
  484.     <h3><a name="defaultdifferentports" id="defaultdifferentports"><code>_default_</code> vhosts
  485.     for different ports</a></h3>
  486.  
  487.     <p>Same as setup 1, but the server listens on several ports and we want
  488.     to use a second <code>_default_</code> vhost for port 80.</p>
  489.  
  490.     <div class="example"><h3>Server configuration</h3><p><code>
  491.     
  492.  
  493.     <VirtualHost _default_:80><br />
  494.     <span class="indent">
  495.         DocumentRoot /www/default80<br />
  496.         # ...<br />
  497.     </span>
  498.     </VirtualHost><br />
  499.         <br />
  500.     <VirtualHost _default_:*><br />
  501.     <span class="indent">
  502.         DocumentRoot /www/default<br />
  503.         # ...<br />
  504.     </span>
  505.     </VirtualHost>
  506.     </code></p></div>
  507.  
  508.     <p>The default vhost for port 80 (which <em>must</em> appear before any
  509.     default vhost with a wildcard port) catches all requests that were sent
  510.     to an unspecified IP address. The main server is never used to serve a
  511.     request.</p>
  512.     
  513.  
  514.     <h3><a name="defaultoneport" id="defaultoneport"><code>_default_</code> vhosts
  515.     for one port</a></h3>
  516.  
  517.     <p>We want to have a default vhost for port 80, but no other default
  518.     vhosts.</p>
  519.  
  520.     <div class="example"><h3>Server configuration</h3><p><code>
  521.     
  522.  
  523.     <VirtualHost _default_:80><br />
  524.     DocumentRoot /www/default<br />
  525.     ...<br />
  526.     </VirtualHost>
  527.     </code></p></div>
  528.  
  529.     <p>A request to an unspecified address on port 80 is served from the
  530.     default vhost. Any other request to an unspecified address and port is
  531.     served from the main server.</p>
  532.     
  533.  
  534.     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  535. <div class="section">
  536. <h2><a name="migrate" id="migrate">Migrating a name-based vhost to an
  537.     IP-based vhost</a></h2>
  538.  
  539.     <p>The name-based vhost with the hostname
  540.     <code>www.example.org</code> (from our <a href="#name">name-based</a> example, setup 2) should get its own IP
  541.     address. To avoid problems with name servers or proxies who cached the
  542.     old IP address for the name-based vhost we want to provide both
  543.     variants during a migration phase.</p>
  544.  
  545.     <p>
  546.      The solution is easy, because we can simply add the new IP address
  547.     (<code>172.20.30.50</code>) to the <code>VirtualHost</code>
  548.     directive.</p>
  549.  
  550.     <div class="example"><h3>Server configuration</h3><p><code>
  551.     
  552.  
  553.     Listen 80<br />
  554.     ServerName www.example.com<br />
  555.     DocumentRoot /www/example1<br />
  556.         <br />
  557.     NameVirtualHost 172.20.30.40<br />
  558.         <br />
  559.     <VirtualHost 172.20.30.40 172.20.30.50><br />
  560.     <span class="indent">
  561.         DocumentRoot /www/example2<br />
  562.         ServerName www.example.org<br />
  563.         # ...<br />
  564.     </span>
  565.     </VirtualHost><br />
  566.         <br />
  567.     <VirtualHost 172.20.30.40><br />
  568.     <span class="indent">
  569.         DocumentRoot /www/example3<br />
  570.         ServerName www.example.net<br />
  571.         ServerAlias *.example.net<br />
  572.         # ...<br />
  573.     </span>
  574.     </VirtualHost>
  575.     </code></p></div>
  576.  
  577.     <p>The vhost can now be accessed through the new address (as an
  578.     IP-based vhost) and through the old address (as a name-based
  579.     vhost).</p>
  580.  
  581.     </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
  582. <div class="section">
  583. <h2><a name="serverpath" id="serverpath">Using the <code>ServerPath</code>
  584.     directive</a></h2>
  585.  
  586.     <p>We have a server with two name-based vhosts. In order to match the
  587.     correct virtual host a client must send the correct <code>Host:</code>
  588.     header. Old HTTP/1.0 clients do not send such a header and Apache has
  589.     no clue what vhost the client tried to reach (and serves the request
  590.     from the primary vhost). To provide as much backward compatibility as
  591.     possible we create a primary vhost which returns a single page
  592.     containing links with an URL prefix to the name-based virtual
  593.     hosts.</p>
  594.  
  595.     <div class="example"><h3>Server configuration</h3><p><code>
  596.     
  597.  
  598.     NameVirtualHost 172.20.30.40<br />
  599.         <br />
  600.     <VirtualHost 172.20.30.40><br />
  601.     <span class="indent">
  602.         # primary vhost<br />
  603.         DocumentRoot /www/subdomain<br />
  604.         RewriteEngine On<br />
  605.         RewriteRule ^/.* /www/subdomain/index.html<br />
  606.         # ...<br />
  607.     </span>
  608.     </VirtualHost><br />
  609.         <br />
  610.     <VirtualHost 172.20.30.40><br />
  611.     DocumentRoot /www/subdomain/sub1<br />
  612.     <span class="indent">
  613.         ServerName www.sub1.domain.tld<br />
  614.         ServerPath /sub1/<br />
  615.         RewriteEngine On<br />
  616.         RewriteRule ^(/sub1/.*) /www/subdomain$1<br />
  617.         # ...<br />
  618.     </span>
  619.     </VirtualHost><br />
  620.         <br />
  621.     <VirtualHost 172.20.30.40><br />
  622.     <span class="indent">
  623.         DocumentRoot /www/subdomain/sub2<br />
  624.         ServerName www.sub2.domain.tld<br />
  625.         ServerPath /sub2/<br />
  626.         RewriteEngine On<br />
  627.         RewriteRule ^(/sub2/.*) /www/subdomain$1<br />
  628.         # ...<br />
  629.     </span>
  630.     </VirtualHost>
  631.     </code></p></div>
  632.  
  633.     <p>Due to the <code class="directive"><a href="../mod/core.html#serverpath">ServerPath</a></code>
  634.     directive a request to the URL
  635.     <code>http://www.sub1.domain.tld/sub1/</code> is <em>always</em> served
  636.     from the sub1-vhost.<br /> A request to the URL
  637.     <code>http://www.sub1.domain.tld/</code> is only
  638.     served from the sub1-vhost if the client sent a correct
  639.     <code>Host:</code> header. If no <code>Host:</code> header is sent the
  640.     client gets the information page from the primary host.</p>
  641.  
  642.     <p>Please note that there is one oddity: A request to
  643.     <code>http://www.sub2.domain.tld/sub1/</code> is also served from the
  644.     sub1-vhost if the client sent no <code>Host:</code> header.</p>
  645.  
  646.     <p>The <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> directives
  647.     are used to make sure that a client which sent a correct
  648.     <code>Host:</code> header can use both URL variants, <em>i.e.</em>,
  649.     with or without URL prefix.</p>
  650.  
  651.     </div></div>
  652. <div class="bottomlang">
  653. <p><span>Available Languages: </span><a href="../en/vhosts/examples.html" title="English"> en </a> |
  654. <a href="../fr/vhosts/examples.html" hreflang="fr" rel="alternate" title="Franτais"> fr </a> |
  655. <a href="../ja/vhosts/examples.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |
  656. <a href="../ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
  657. </div><div id="footer">
  658. <p class="apache">Copyright 2007 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
  659. <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div>
  660. </body></html>