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