github.com/krum110487/go-htaccess@v0.0.0-20240316004156-60641c8e7598/tests/data/apache_2_2_34/manual/rewrite/vhosts.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>Dynamic mass virtual hosts with mod_rewrite - 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/rewrite/vhosts.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="./">Rewrite</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/rewrite/vhosts.html">this link</a> to go to the current version of this document.</p></div><div id="preamble"><h1>Dynamic mass virtual hosts with mod_rewrite</h1> 27 <div class="toplang"> 28 <p><span>Available Languages: </span><a href="../en/rewrite/vhosts.html" title="English"> en </a></p> 29 </div> 30 31 32 <p>This document supplements the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> 33 <a href="../mod/mod_rewrite.html">reference documentation</a>. It describes 34 how you can use <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> to create dynamically 35 configured virtual hosts.</p> 36 37 <div class="warning">mod_rewrite is not the best way to configure 38 virtual hosts. You should first consider the <a href="../vhosts/mass.html">alternatives</a> before resorting to 39 mod_rewrite. See also the "<a href="avoid.html#vhosts">how to avoid 40 mod_rewrite</a> document.</div> 41 42 </div> 43 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#per-hostname">Virtual Hosts For Arbitrary Hostnames</a></li> 44 <li><img alt="" src="../images/down.gif" /> <a href="#simple.rewrite">Dynamic 45 Virtual Hosts Using <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code></a></li> 46 <li><img alt="" src="../images/down.gif" /> <a href="#xtra-conf">Using a Separate Virtual Host Configuration File</a></li> 47 </ul><h3>See also</h3><ul class="seealso"><li><a href="../mod/mod_rewrite.html">Module documentation</a></li><li><a href="intro.html">mod_rewrite introduction</a></li><li><a href="remapping.html">Redirection and remapping</a></li><li><a href="access.html">Controlling access</a></li><li><a href="proxy.html">Proxying</a></li><li><a href="rewritemap.html">RewriteMap</a></li><li><a href="advanced.html">Advanced techniques and tricks</a></li><li><a href="avoid.html">When not to use mod_rewrite</a></li></ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div> 48 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> 49 <div class="section"> 50 <h2><a name="per-hostname" id="per-hostname">Virtual Hosts For Arbitrary Hostnames</a></h2> 51 52 53 54 <dl> 55 <dt>Description:</dt> 56 57 <dd> 58 <p>We want to automatically create a virtual host for every hostname 59 which resolves in our domain, without having to create 60 new VirtualHost sections.</p> 61 62 <p>In this recipe, we assume that we'll be using the hostname 63 <code>www.<strong>SITE</strong>.example.com</code> for each 64 user, and serve their content out of 65 <code>/home/<strong>SITE</strong>/www</code>.</p> 66 </dd> 67 68 <dt>Solution:</dt> 69 70 <dd> 71 72 <div class="example"><p><code> 73 RewriteEngine on<br /> 74 <br /> 75 RewriteMap lowercase int:tolower<br /> 76 <br /> 77 RewriteCond %{lowercase:%{<strong>HTTP_HOST</strong>}} ^www\.<strong>([^.]+)</strong>\.example\.com$<br /> 78 RewriteRule ^(.*) /home/<strong>%1</strong>/www$1 79 </code></p></div></dd> 80 81 <dt>Discussion</dt> 82 <dd> 83 84 <div class="warning">You will need to take care of the DNS 85 resolution - Apache does 86 not handle name resolution. You'll need either to create CNAME 87 records for each hostname, or a DNS wildcard record. Creating DNS 88 records is beyond the scope of this document.</div> 89 90 <p>The internal <code>tolower</code> RewriteMap directive is used to 91 ensure that the hostnames being used are all lowercase, so that there is 92 no ambiguity in the directory structure which must be created.</p> 93 94 <p>Parentheses used in a <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code> are captured into the 95 backreferences <code>%1</code>, <code>%2</code>, etc, while parentheses 96 used in <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> are 97 captured into the backreferences <code>$1</code>, <code>$2</code>, 98 etc.</p> 99 100 <p> 101 As with many techniques discussed in this document, mod_rewrite really 102 isn't the best way to accomplish this task. You should, instead, 103 consider using <code class="module"><a href="../mod/mod_vhost_alias.html">mod_vhost_alias</a></code> instead, as it will much 104 more gracefully handle anything beyond serving static files, such as any 105 dynamic content, and Alias resolution. 106 </p> 107 </dd> 108 </dl> 109 110 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> 111 <div class="section"> 112 <h2><a name="simple.rewrite" id="simple.rewrite">Dynamic 113 Virtual Hosts Using <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code></a></h2> 114 115 <p>This extract from <code>httpd.conf</code> does the same 116 thing as <a href="#per-hostname">the first example</a>. The first 117 half is very similar to the corresponding part above, except for 118 some changes, required for backward compatibility and to make the 119 <code>mod_rewrite</code> part work properly; the second half 120 configures <code>mod_rewrite</code> to do the actual work.</p> 121 122 <p>Because <code>mod_rewrite</code> runs before other URI translation 123 modules (e.g., <code>mod_alias</code>), <code>mod_rewrite</code> must 124 be told to explicitly ignore any URLs that would have been handled 125 by those modules. And, because these rules would otherwise bypass 126 any <code>ScriptAlias</code> directives, we must have 127 <code>mod_rewrite</code> explicitly enact those mappings.</p> 128 129 <div class="example"><p><code> 130 # get the server name from the Host: header<br /> 131 UseCanonicalName Off<br /> 132 <br /> 133 # splittable logs<br /> 134 LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon<br /> 135 CustomLog logs/access_log vcommon<br /> 136 <br /> 137 <Directory /www/hosts><br /> 138 <span class="indent"> 139 # ExecCGI is needed here because we can't force<br /> 140 # CGI execution in the way that ScriptAlias does<br /> 141 Options FollowSymLinks ExecCGI<br /> 142 </span> 143 </Directory><br /> 144 <br /> 145 RewriteEngine On<br /> 146 <br /> 147 # a ServerName derived from a Host: header may be any case at all<br /> 148 RewriteMap lowercase int:tolower<br /> 149 <br /> 150 ## deal with normal documents first:<br /> 151 # allow Alias /icons/ to work - repeat for other aliases<br /> 152 RewriteCond %{REQUEST_URI} !^/icons/<br /> 153 # allow CGIs to work<br /> 154 RewriteCond %{REQUEST_URI} !^/cgi-bin/<br /> 155 # do the magic<br /> 156 RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1<br /> 157 <br /> 158 ## and now deal with CGIs - we have to force a handler<br /> 159 RewriteCond %{REQUEST_URI} ^/cgi-bin/<br /> 160 RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1 [H=cgi-script]<br /> 161 </code></p></div> 162 163 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> 164 <div class="section"> 165 <h2><a name="xtra-conf" id="xtra-conf">Using a Separate Virtual Host Configuration File</a></h2> 166 167 <p>This arrangement uses more advanced <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> 168 features to work out the translation from virtual host to document 169 root, from a separate configuration file. This provides more 170 flexibility, but requires more complicated configuration.</p> 171 172 <p>The <code>vhost.map</code> file should look something like 173 this:</p> 174 175 <div class="example"><p><code> 176 customer-1.example.com /www/customers/1<br /> 177 customer-2.example.com /www/customers/2<br /> 178 # ...<br /> 179 customer-N.example.com /www/customers/N<br /> 180 </code></p></div> 181 182 <p>The <code>httpd.conf</code> should contain the following:</p> 183 184 <div class="example"><p><code> 185 RewriteEngine on<br /> 186 <br /> 187 RewriteMap lowercase int:tolower<br /> 188 <br /> 189 # define the map file<br /> 190 RewriteMap vhost txt:/www/conf/vhost.map<br /> 191 <br /> 192 # deal with aliases as above<br /> 193 RewriteCond %{REQUEST_URI} !^/icons/<br /> 194 RewriteCond %{REQUEST_URI} !^/cgi-bin/<br /> 195 RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$<br /> 196 # this does the file-based remap<br /> 197 RewriteCond ${vhost:%1} ^(/.*)$<br /> 198 RewriteRule ^/(.*)$ %1/docs/$1<br /> 199 <br /> 200 RewriteCond %{REQUEST_URI} ^/cgi-bin/<br /> 201 RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$<br /> 202 RewriteCond ${vhost:%1} ^(/.*)$<br /> 203 RewriteRule ^/(.*)$ %1/cgi-bin/$1 [H=cgi-script] 204 </code></p></div> 205 206 </div></div> 207 <div class="bottomlang"> 208 <p><span>Available Languages: </span><a href="../en/rewrite/vhosts.html" title="English"> en </a></p> 209 </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> 210 <script type="text/javascript"><!--//--><![CDATA[//><!-- 211 var comments_shortname = 'httpd'; 212 var comments_identifier = 'http://httpd.apache.org/docs/2.2/rewrite/vhosts.html'; 213 (function(w, d) { 214 if (w.location.hostname.toLowerCase() == "httpd.apache.org") { 215 d.write('<div id="comments_thread"><\/div>'); 216 var s = d.createElement('script'); 217 s.type = 'text/javascript'; 218 s.async = true; 219 s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier; 220 (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s); 221 } 222 else { 223 d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>'); 224 } 225 })(window, document); 226 //--><!]]></script></div><div id="footer"> 227 <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> 228 <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[//><!-- 229 if (typeof(prettyPrint) !== 'undefined') { 230 prettyPrint(); 231 } 232 //--><!]]></script> 233 </body></html>