summaryrefslogtreecommitdiffstats
path: root/docs/manual/vhosts/examples.html.en
blob: b2113a2aaf9b96a15664169bdd0f26e5cf5d52b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
              This file is generated from xml source: DO NOT EDIT
        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      -->
<title>VirtualHost Examples - Apache HTTP Server</title>
<link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
<link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
<link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
<link href="../images/favicon.ico" rel="shortcut icon" /></head>
<body id="manual-page"><div id="page-header">
<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>
<p class="apache">Apache HTTP Server Version 2.3</p>
<img alt="" src="../images/feather.gif" /></div>
<div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
<div id="path">
<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>
<div class="toplang">
<p><span>Available Languages: </span><a href="../en/vhosts/examples.html" title="English">&nbsp;en&nbsp;</a> |
<a href="../fr/vhosts/examples.html" hreflang="fr" rel="alternate" title="Fran�ais">&nbsp;fr&nbsp;</a> |
<a href="../ja/vhosts/examples.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
<a href="../ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a> |
<a href="../tr/vhosts/examples.html" hreflang="tr" rel="alternate" title="T�rk�e">&nbsp;tr&nbsp;</a></p>
</div>


    <p>This document attempts to answer the commonly-asked questions about
    setting up <a href="index.html">virtual hosts</a>. These scenarios are those involving multiple
    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.
    </p>

</div>
<div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#purename">Running several name-based web
    sites on a single IP address.</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#twoips">Name-based hosts on more than one
    IP address.</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#intraextra">Serving the same content on
    different IP addresses (such as an internal and external
    address).</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#port">Running different sites on different
    ports.</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#ip">IP-based virtual hosting</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#ipport">Mixed port-based and ip-based virtual
  hosts</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#mixed">Mixed name-based and IP-based
    vhosts</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#proxy">Using <code>Virtual_host</code> and
    mod_proxy together</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#default">Using <code>_default_</code>
    vhosts</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#migrate">Migrating a name-based vhost to an
    IP-based vhost</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#serverpath">Using the <code>ServerPath</code>
  directive</a></li>
</ul></div>
<div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="purename" id="purename">Running several name-based web
    sites on a single IP address.</a></h2>

    <p>Your server has a single IP address, and multiple aliases (CNAMES)
    point to this machine in DNS. You want to run a web server for
    <code>www.example.com</code> and <code>www.example.org</code> on this
    machine.</p>

    <div class="note"><h3>Note</h3><p>Creating virtual
          host configurations on your Apache server does not magically
          cause DNS entries to be created for those host names. You
          <em>must</em> have the names in DNS, resolving to your IP
          address, or nobody else will be able to see your web site. You
          can put entries in your <code>hosts</code> file for local
          testing, but that will work only from the machine with those
          <code>hosts</code> entries.</p>
    </div>

    <div class="example"><h3>Server configuration</h3><p><code>
    

    # Ensure that Apache listens on port 80<br />
    Listen 80<br />
    <br />
    # Listen for virtual host requests on all IP addresses<br />
    NameVirtualHost *:80<br />
    <br />
    &lt;VirtualHost *:80&gt;<br />
    <span class="indent">
      DocumentRoot /www/example1<br />
      ServerName www.example.com<br />
      <br />
      # Other directives here<br />
      <br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost *:80&gt;<br />
    <span class="indent">
      DocumentRoot /www/example2<br />
      ServerName www.example.org<br />
      <br />
      # Other directives here<br />
      <br />
    </span>
    &lt;/VirtualHost&gt;
    </code></p></div>

    <p>The asterisks match all addresses, so the main server serves no
    requests. Due to the fact that the virtual host with
    <code>ServerName www.example.com</code> is first
    in the configuration file, it has the highest priority and can be seen
    as the <cite>default</cite> or <cite>primary</cite> server. That means
    that if a request is received that does not match one of the specified
    <code>ServerName</code> directives, it will be served by this first
    <code>VirtualHost</code>.</p>

    <div class="note">
            <h3>Note</h3>

            <p>You can, if you wish, replace <code>*</code> with the actual
            IP address of the system. In that case, the argument to
            <code>VirtualHost</code> <em>must</em> match the argument to
            <code>NameVirtualHost</code>:</p>

            <div class="example"><p><code>
            NameVirtualHost 172.20.30.40<br />
            <br />
            &lt;VirtualHost 172.20.30.40&gt;<br />
             # etc ...
            </code></p></div>

           <p>However, it is additionally useful to use <code>*</code>
           on systems where the IP address is not predictable - for
           example if you have a dynamic IP address with your ISP, and
           you are using some variety of dynamic DNS solution. Since
           <code>*</code> matches any IP address, this configuration
           would work without changes whenever your IP address
           changes.</p>
    </div>

    <p>The above configuration is what you will want to use in almost
    all name-based virtual hosting situations. The only thing that this
    configuration will not work for, in fact, is when you are serving
    different content based on differing IP addresses or ports.</p>

  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="twoips" id="twoips">Name-based hosts on more than one
    IP address.</a></h2>

    <div class="note">
      <h3>Note</h3>
      <p>Any of the techniques discussed here can be extended to any
      number of IP addresses.</p>
    </div>

    <p>The server has two IP addresses. On one (<code>172.20.30.40</code>), we
    will serve the "main" server, <code>server.example.com</code> and on the
    other (<code>172.20.30.50</code>), we will serve two or more virtual hosts.</p>

    <div class="example"><h3>Server configuration</h3><p><code>
    

    Listen 80<br />
    <br />
    # This is the "main" server running on 172.20.30.40<br />
    ServerName server.example.com<br />
    DocumentRoot /www/mainserver<br />
    <br />
    # This is the other address<br />
    NameVirtualHost 172.20.30.50<br />
    <br />
    &lt;VirtualHost 172.20.30.50&gt;<br />
    <span class="indent">
        DocumentRoot /www/example1<br />
        ServerName www.example.com<br />
        <br />
        # Other directives here ...<br />
        <br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost 172.20.30.50&gt;<br />
    <span class="indent">
        DocumentRoot /www/example2<br />
        ServerName www.example.org<br />
        <br />
        # Other directives here ...<br />
        <br />
    </span>
    &lt;/VirtualHost&gt;
    </code></p></div>

    <p>Any request to an address other than <code>172.20.30.50</code> will be
    served from the main server. A request to <code>172.20.30.50</code> with an
    unknown hostname, or no <code>Host:</code> header, will be served from
    <code>www.example.com</code>.</p>

  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="intraextra" id="intraextra">Serving the same content on
    different IP addresses (such as an internal and external
    address).</a></h2>

    <p>The server machine has two IP addresses (<code>192.168.1.1</code>
    and <code>172.20.30.40</code>). The machine is sitting between an
    internal (intranet) network and an external (internet) network. Outside
    of the network, the name <code>server.example.com</code> resolves to
    the external address (<code>172.20.30.40</code>), but inside the
    network, that same name resolves to the internal address
    (<code>192.168.1.1</code>).</p>

    <p>The server can be made to respond to internal and external requests
    with the same content, with just one <code>VirtualHost</code>
    section.</p>

    <div class="example"><h3>Server configuration</h3><p><code>
    

    NameVirtualHost 192.168.1.1<br />
    NameVirtualHost 172.20.30.40<br />
    <br />
    &lt;VirtualHost 192.168.1.1 172.20.30.40&gt;<br />
    <span class="indent">
        DocumentRoot /www/server1<br />
        ServerName server.example.com<br />
        ServerAlias server<br />
    </span>
    &lt;/VirtualHost&gt;
    </code></p></div>

    <p>Now requests from both networks will be served from the same
    <code>VirtualHost</code>.</p>

    <div class="note">
          <h3>Note:</h3><p>On the internal
          network, one can just use the name <code>server</code> rather
          than the fully qualified host name
          <code>server.example.com</code>.</p>

          <p>Note also that, in the above example, you can replace the list
          of IP addresses with <code>*</code>, which will cause the server to
          respond the same on all addresses.</p>
    </div>

  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="port" id="port">Running different sites on different
    ports.</a></h2>

    <p>You have multiple domains going to the same IP and also want to
    serve multiple ports. By defining the ports in the "NameVirtualHost"
    tag, you can allow this to work. If you try using &lt;VirtualHost
    name:port&gt; without the NameVirtualHost name:port or you try to use
    the Listen directive, your configuration will not work.</p>

    <div class="example"><h3>Server configuration</h3><p><code>
    

    Listen 80<br />
    Listen 8080<br />
    <br />
    NameVirtualHost 172.20.30.40:80<br />
    NameVirtualHost 172.20.30.40:8080<br />
    <br />
    &lt;VirtualHost 172.20.30.40:80&gt;<br />
    <span class="indent">
        ServerName www.example.com<br />
        DocumentRoot /www/domain-80<br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost 172.20.30.40:8080&gt;<br />
    <span class="indent">
        ServerName www.example.com<br />
        DocumentRoot /www/domain-8080<br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost 172.20.30.40:80&gt;<br />
    <span class="indent">
        ServerName www.example.org<br />
        DocumentRoot /www/otherdomain-80<br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost 172.20.30.40:8080&gt;<br />
    <span class="indent">
        ServerName www.example.org<br />
        DocumentRoot /www/otherdomain-8080<br />
    </span>
    &lt;/VirtualHost&gt;
    </code></p></div>

  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="ip" id="ip">IP-based virtual hosting</a></h2>

    <p>The server has two IP addresses (<code>172.20.30.40</code> and
    <code>172.20.30.50</code>) which resolve to the names
    <code>www.example.com</code> and <code>www.example.org</code>
    respectively.</p>

    <div class="example"><h3>Server configuration</h3><p><code>
    

    Listen 80<br />
    <br />
    &lt;VirtualHost 172.20.30.40&gt;<br />
    <span class="indent">
        DocumentRoot /www/example1<br />
        ServerName www.example.com<br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost 172.20.30.50&gt;<br />
    <span class="indent">
        DocumentRoot /www/example2<br />
        ServerName www.example.org<br />
    </span>
    &lt;/VirtualHost&gt;
    </code></p></div>

    <p>Requests for any address not specified in one of the
    <code>&lt;VirtualHost&gt;</code> directives (such as
    <code>localhost</code>, for example) will go to the main server, if
    there is one.</p>

  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="ipport" id="ipport">Mixed port-based and ip-based virtual
  hosts</a></h2>

    <p>The server machine has two IP addresses (<code>172.20.30.40</code> and
    <code>172.20.30.50</code>) which resolve to the names
    <code>www.example.com</code> and <code>www.example.org</code>
    respectively. In each case, we want to run hosts on ports 80 and
    8080.</p>

    <div class="example"><h3>Server configuration</h3><p><code>
    

    Listen 172.20.30.40:80<br />
    Listen 172.20.30.40:8080<br />
    Listen 172.20.30.50:80<br />
    Listen 172.20.30.50:8080<br />
    <br />
    &lt;VirtualHost 172.20.30.40:80&gt;<br />
    <span class="indent">
        DocumentRoot /www/example1-80<br />
        ServerName www.example.com<br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost 172.20.30.40:8080&gt;<br />
    <span class="indent">
        DocumentRoot /www/example1-8080<br />
        ServerName www.example.com<br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost 172.20.30.50:80&gt;<br />
    <span class="indent">
        DocumentRoot /www/example2-80<br />
        ServerName www.example.org<br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost 172.20.30.50:8080&gt;<br />
    <span class="indent">
        DocumentRoot /www/example2-8080<br />
        ServerName www.example.org<br />
    </span>
    &lt;/VirtualHost&gt;
    </code></p></div>

  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="mixed" id="mixed">Mixed name-based and IP-based
    vhosts</a></h2>

    <p>On some of my addresses, I want to do name-based virtual hosts, and
    on others, IP-based hosts.</p>

    <div class="example"><h3>Server configuration</h3><p><code>
    

    Listen 80<br />
    <br />
    NameVirtualHost 172.20.30.40<br />
    <br />
    &lt;VirtualHost 172.20.30.40&gt;<br />
    <span class="indent">
        DocumentRoot /www/example1<br />
        ServerName www.example.com<br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost 172.20.30.40&gt;<br />
    <span class="indent">
        DocumentRoot /www/example2<br />
        ServerName www.example.org<br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost 172.20.30.40&gt;<br />
    <span class="indent">
        DocumentRoot /www/example3<br />
        ServerName www.example.net<br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    # IP-based<br />
    &lt;VirtualHost 172.20.30.50&gt;<br />
    <span class="indent">
        DocumentRoot /www/example4<br />
        ServerName www.example.edu<br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost 172.20.30.60&gt;<br />
    <span class="indent">
        DocumentRoot /www/example5<br />
        ServerName www.example.gov<br />
    </span>
    &lt;/VirtualHost&gt;
    </code></p></div>

  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="proxy" id="proxy">Using <code>Virtual_host</code> and
    mod_proxy together</a></h2>

    <p>The following example allows a front-end machine to proxy a
    virtual host through to a server running on another machine. In the
    example, a virtual host of the same name is configured on a machine
    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
    used so that the desired hostname is passed through, in case we are
    proxying multiple hostnames to a single machine.</p>

    <div class="example"><p><code>
    &lt;VirtualHost *:*&gt;<br />
        ProxyPreserveHost On<br />
        ProxyPass / http://192.168.111.2/<br />
        ProxyPassReverse / http://192.168.111.2/<br />
        ServerName hostname.example.com<br />
    &lt;/VirtualHost&gt;
    </code></p></div>

    </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="default" id="default">Using <code>_default_</code>
    vhosts</a></h2>

    <h3><a name="defaultallports" id="defaultallports"><code>_default_</code> vhosts
    for all ports</a></h3>

    <p>Catching <em>every</em> request to any unspecified IP address and
    port, <em>i.e.</em>, an address/port combination that is not used for
    any other virtual host.</p>

    <div class="example"><h3>Server configuration</h3><p><code>
    

    &lt;VirtualHost _default_:*&gt;<br />
    <span class="indent">
        DocumentRoot /www/default<br />
    </span>
    &lt;/VirtualHost&gt;
    </code></p></div>

    <p>Using such a default vhost with a wildcard port effectively prevents
    any request going to the main server.</p>

    <p>A default vhost never serves a request that was sent to an
    address/port that is used for name-based vhosts. If the request
    contained an unknown or no <code>Host:</code> header it is always
    served from the primary name-based vhost (the vhost for that
    address/port appearing first in the configuration file).</p>

    <p>You can use <code class="directive"><a href="../mod/mod_alias.html#aliasmatch">AliasMatch</a></code> or
    <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> to rewrite any
    request to a single information page (or script).</p>
    

    <h3><a name="defaultdifferentports" id="defaultdifferentports"><code>_default_</code> vhosts
    for different ports</a></h3>

    <p>Same as setup 1, but the server listens on several ports and we want
    to use a second <code>_default_</code> vhost for port 80.</p>

    <div class="example"><h3>Server configuration</h3><p><code>
    

    &lt;VirtualHost _default_:80&gt;<br />
    <span class="indent">
        DocumentRoot /www/default80<br />
        # ...<br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost _default_:*&gt;<br />
    <span class="indent">
        DocumentRoot /www/default<br />
        # ...<br />
    </span>
    &lt;/VirtualHost&gt;
    </code></p></div>

    <p>The default vhost for port 80 (which <em>must</em> appear before any
    default vhost with a wildcard port) catches all requests that were sent
    to an unspecified IP address. The main server is never used to serve a
    request.</p>
    

    <h3><a name="defaultoneport" id="defaultoneport"><code>_default_</code> vhosts
    for one port</a></h3>

    <p>We want to have a default vhost for port 80, but no other default
    vhosts.</p>

    <div class="example"><h3>Server configuration</h3><p><code>
    

    &lt;VirtualHost _default_:80&gt;<br />
    DocumentRoot /www/default<br />
    ...<br />
    &lt;/VirtualHost&gt;
    </code></p></div>

    <p>A request to an unspecified address on port 80 is served from the
    default vhost. Any other request to an unspecified address and port is
    served from the main server.</p>

    <p>Any use of <code>*</code> in a virtual host declaration will have
    higher precedence than <code>_default_</code>.</p>

    

  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="migrate" id="migrate">Migrating a name-based vhost to an
    IP-based vhost</a></h2>

    <p>The name-based vhost with the hostname
    <code>www.example.org</code> (from our <a href="#name">name-based</a> example, setup 2) should get its own IP
    address. To avoid problems with name servers or proxies who cached the
    old IP address for the name-based vhost we want to provide both
    variants during a migration phase.</p>

    <p>
     The solution is easy, because we can simply add the new IP address
    (<code>172.20.30.50</code>) to the <code>VirtualHost</code>
    directive.</p>

    <div class="example"><h3>Server configuration</h3><p><code>
    

    Listen 80<br />
    ServerName www.example.com<br />
    DocumentRoot /www/example1<br />
    <br />
    NameVirtualHost 172.20.30.40<br />
    <br />
    &lt;VirtualHost 172.20.30.40 172.20.30.50&gt;<br />
    <span class="indent">
        DocumentRoot /www/example2<br />
        ServerName www.example.org<br />
        # ...<br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost 172.20.30.40&gt;<br />
    <span class="indent">
        DocumentRoot /www/example3<br />
        ServerName www.example.net<br />
        ServerAlias *.example.net<br />
        # ...<br />
    </span>
    &lt;/VirtualHost&gt;
    </code></p></div>

    <p>The vhost can now be accessed through the new address (as an
    IP-based vhost) and through the old address (as a name-based
    vhost).</p>

  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="serverpath" id="serverpath">Using the <code>ServerPath</code>
  directive</a></h2>

    <p>We have a server with two name-based vhosts. In order to match the
    correct virtual host a client must send the correct <code>Host:</code>
    header. Old HTTP/1.0 clients do not send such a header and Apache has
    no clue what vhost the client tried to reach (and serves the request
    from the primary vhost). To provide as much backward compatibility as
    possible we create a primary vhost which returns a single page
    containing links with an URL prefix to the name-based virtual
    hosts.</p>

    <div class="example"><h3>Server configuration</h3><p><code>
    

    NameVirtualHost 172.20.30.40<br />
    <br />
    &lt;VirtualHost 172.20.30.40&gt;<br />
    <span class="indent">
        # primary vhost<br />
        DocumentRoot /www/subdomain<br />
        RewriteEngine On<br />
        RewriteRule ^/.* /www/subdomain/index.html<br />
        # ...<br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost 172.20.30.40&gt;<br />
    DocumentRoot /www/subdomain/sub1<br />
    <span class="indent">
        ServerName www.sub1.domain.tld<br />
        ServerPath /sub1/<br />
        RewriteEngine On<br />
        RewriteRule ^(/sub1/.*) /www/subdomain$1<br />
        # ...<br />
    </span>
    &lt;/VirtualHost&gt;<br />
    <br />
    &lt;VirtualHost 172.20.30.40&gt;<br />
    <span class="indent">
        DocumentRoot /www/subdomain/sub2<br />
        ServerName www.sub2.domain.tld<br />
        ServerPath /sub2/<br />
        RewriteEngine On<br />
        RewriteRule ^(/sub2/.*) /www/subdomain$1<br />
        # ...<br />
    </span>
    &lt;/VirtualHost&gt;
    </code></p></div>

    <p>Due to the <code class="directive"><a href="../mod/core.html#serverpath">ServerPath</a></code>
    directive a request to the URL
    <code>http://www.sub1.domain.tld/sub1/</code> is <em>always</em> served
    from the sub1-vhost.<br /> A request to the URL
    <code>http://www.sub1.domain.tld/</code> is only
    served from the sub1-vhost if the client sent a correct
    <code>Host:</code> header. If no <code>Host:</code> header is sent the
    client gets the information page from the primary host.</p>

    <p>Please note that there is one oddity: A request to
    <code>http://www.sub2.domain.tld/sub1/</code> is also served from the
    sub1-vhost if the client sent no <code>Host:</code> header.</p>

    <p>The <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> directives
    are used to make sure that a client which sent a correct
    <code>Host:</code> header can use both URL variants, <em>i.e.</em>,
    with or without URL prefix.</p>

  </div></div>
<div class="bottomlang">
<p><span>Available Languages: </span><a href="../en/vhosts/examples.html" title="English">&nbsp;en&nbsp;</a> |
<a href="../fr/vhosts/examples.html" hreflang="fr" rel="alternate" title="Fran�ais">&nbsp;fr&nbsp;</a> |
<a href="../ja/vhosts/examples.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
<a href="../ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a> |
<a href="../tr/vhosts/examples.html" hreflang="tr" rel="alternate" title="T�rk�e">&nbsp;tr&nbsp;</a></p>
</div><div id="footer">
<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>
<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>
</body></html>