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