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