]> granicus.if.org Git - apache/blob - docs/manual/rewrite/vhosts.html.en
move es and fr targets to *.utf8 extension. Update transformation
[apache] / docs / manual / rewrite / vhosts.html.en
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
4 <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.5</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" /></head>
18 <body id="manual-page"><div id="page-header">
19 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.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.5</p>
21 <img alt="" src="../images/feather.png" /></div>
22 <div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
23 <div id="path">
24 <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.5</a> &gt; <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>Dynamic mass virtual hosts with mod_rewrite</h1>
25 <div class="toplang">
26 <p><span>Available Languages: </span><a href="../en/rewrite/vhosts.html" title="English">&nbsp;en&nbsp;</a> |
27 <a href="../fr/rewrite/vhosts.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a></p>
28 </div>
29
30
31 <p>This document supplements the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
32 <a href="../mod/mod_rewrite.html">reference documentation</a>. It describes
33 how you can use <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> to create dynamically
34 configured virtual hosts.</p>
35
36 <div class="warning">mod_rewrite is usually not the best way to configure
37 virtual hosts. You should first consider the <a href="../vhosts/mass.html">alternatives</a> before resorting to
38 mod_rewrite. See also the "<a href="avoid.html#vhosts">how to avoid
39 mod_rewrite</a> document.</div>
40
41 </div>
42 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#per-hostname">Virtual Hosts For Arbitrary Hostnames</a></li>
43 <li><img alt="" src="../images/down.gif" /> <a href="#simple.rewrite">Dynamic
44     Virtual Hosts Using <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code></a></li>
45 <li><img alt="" src="../images/down.gif" /> <a href="#xtra-conf">Using a Separate Virtual Host Configuration File</a></li>
46 </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</a></li><li><a href="avoid.html">When not to use mod_rewrite</a></li><li><a href="#comments_section">Comments</a></li></ul></div>
47 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
48 <div class="section">
49 <h2><a name="per-hostname" id="per-hostname">Virtual Hosts For Arbitrary Hostnames</a><a title="Permanent link" href="#per-hostname" class="permalink">&para;</a></h2>
50
51   
52
53   <dl>
54     <dt>Description:</dt>
55
56     <dd>
57     <p>We want to automatically create a virtual host for every hostname
58     which resolves in our domain, without having to create
59     new VirtualHost sections.</p>
60
61     <p>In this recipe, we assume that we'll be using the hostname
62     <code><strong>SITE</strong>.example.com</code> for each
63     user, and serve their content out of
64     <code>/home/<strong>SITE</strong>/www</code>. However, we want
65     <code>www.example.com</code> to be ommitted from this mapping.</p>
66     </dd>
67
68     <dt>Solution:</dt>
69
70     <dd>
71
72 <pre class="prettyprint lang-config">RewriteEngine on
73
74 RewriteMap    lowercase int:tolower
75
76 RewriteCond   %{HTTP_HOST} !^www\.
77 RewriteCond   ${lowercase:%{<strong>HTTP_HOST</strong>}}   ^<strong>([^.]+)</strong>\.example\.com$
78 RewriteRule   ^(.*)    /home/<strong>%1</strong>/www$1</pre>
79 </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>The first <code>RewriteCond</code> checks to see if the hostname
101 starts with <code>www.</code>, and if it does, the rewriting is
102 skipped.</p>
103
104 <p>
105 As with many techniques discussed in this document, mod_rewrite really
106 isn't the best way to accomplish this task. You should, instead,
107 consider using <code class="module"><a href="../mod/mod_vhost_alias.html">mod_vhost_alias</a></code> instead, as it will much
108 more gracefully handle anything beyond serving static files, such as any
109 dynamic content, and Alias resolution.
110 </p>
111     </dd>
112   </dl>
113
114 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
115 <div class="section">
116 <h2><a name="simple.rewrite" id="simple.rewrite">Dynamic
117     Virtual Hosts Using <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code></a><a title="Permanent link" href="#simple.rewrite" class="permalink">&para;</a></h2>
118
119     <p>This extract from <code>httpd.conf</code> does the same
120     thing as <a href="#per-hostname">the first example</a>. The first
121     half is very similar to the corresponding part above, except for
122     some changes, required for backward compatibility and to make the
123     <code>mod_rewrite</code> part work properly; the second half
124     configures <code>mod_rewrite</code> to do the actual work.</p>
125
126     <p>Because <code>mod_rewrite</code> runs before other URI translation
127     modules (e.g., <code>mod_alias</code>), <code>mod_rewrite</code> must
128     be told to explicitly ignore any URLs that would have been handled
129     by those modules. And, because these rules would otherwise bypass
130     any <code>ScriptAlias</code> directives, we must have
131     <code>mod_rewrite</code> explicitly enact those mappings.</p>
132
133 <pre class="prettyprint lang-config"># get the server name from the Host: header
134 UseCanonicalName Off
135
136 # splittable logs
137 LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
138 CustomLog "logs/access_log" vcommon
139
140 &lt;Directory "/www/hosts"&gt;
141     # ExecCGI is needed here because we can't force
142     # CGI execution in the way that ScriptAlias does
143     Options FollowSymLinks ExecCGI
144 &lt;/Directory&gt;
145
146 RewriteEngine On
147
148 # a ServerName derived from a Host: header may be any case at all
149 RewriteMap  lowercase  "int:tolower"
150
151 ## deal with normal documents first:
152 # allow Alias /icons/ to work - repeat for other aliases
153 RewriteCond  "%{REQUEST_URI}"  "!^/icons/"
154 # allow CGIs to work
155 RewriteCond  "%{REQUEST_URI}"  "!^/cgi-bin/"
156 # do the magic
157 RewriteRule  "^/(.*)$"         "/www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1"
158
159 ## and now deal with CGIs - we have to force a handler
160 RewriteCond  "%{REQUEST_URI}"  "^/cgi-bin/"
161 RewriteRule  "^/(.*)$"         "/www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1"  [H=cgi-script]</pre>
162
163
164 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
165 <div class="section">
166 <h2><a name="xtra-conf" id="xtra-conf">Using a Separate Virtual Host Configuration File</a><a title="Permanent link" href="#xtra-conf" class="permalink">&para;</a></h2>
167
168     <p>This arrangement uses more advanced <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
169     features to work out the translation from virtual host to document
170     root, from a separate configuration file. This provides more
171     flexibility, but requires more complicated configuration.</p>
172
173     <p>The <code>vhost.map</code> file should look something like
174     this:</p>
175
176 <div class="example"><p><code>
177 customer-1.example.com  /www/customers/1<br />
178 customer-2.example.com  /www/customers/2<br />
179 # ...<br />
180 customer-N.example.com  /www/customers/N<br />
181 </code></p></div>
182
183     <p>The <code>httpd.conf</code> should contain the following:</p>
184
185 <pre class="prettyprint lang-config">RewriteEngine on
186
187 RewriteMap   lowercase  "int:tolower"
188
189 # define the map file
190 RewriteMap   vhost      "txt:/www/conf/vhost.map"
191
192 # deal with aliases as above
193 RewriteCond  "%{REQUEST_URI}"               "!^/icons/"
194 RewriteCond  "%{REQUEST_URI}"               "!^/cgi-bin/"
195 RewriteCond  "${lowercase:%{SERVER_NAME}}"  "^(.+)$"
196 # this does the file-based remap
197 RewriteCond  "${vhost:%1}"                  "^(/.*)$"
198 RewriteRule  "^/(.*)$"                      "%1/docs/$1"
199
200 RewriteCond  "%{REQUEST_URI}"               "^/cgi-bin/"
201 RewriteCond  "${lowercase:%{SERVER_NAME}}"  "^(.+)$"
202 RewriteCond  "${vhost:%1}"                  "^(/.*)$"
203 RewriteRule  "^/cgi-bin/(.*)$"                      "%1/cgi-bin/$1" [H=cgi-script]</pre>
204
205
206 </div></div>
207 <div class="bottomlang">
208 <p><span>Available Languages: </span><a href="../en/rewrite/vhosts.html" title="English">&nbsp;en&nbsp;</a> |
209 <a href="../fr/rewrite/vhosts.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a></p>
210 </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>
211 <script type="text/javascript"><!--//--><![CDATA[//><!--
212 var comments_shortname = 'httpd';
213 var comments_identifier = 'http://httpd.apache.org/docs/trunk/rewrite/vhosts.html';
214 (function(w, d) {
215     if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
216         d.write('<div id="comments_thread"><\/div>');
217         var s = d.createElement('script');
218         s.type = 'text/javascript';
219         s.async = true;
220         s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
221         (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
222     }
223     else {
224         d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
225     }
226 })(window, document);
227 //--><!]]></script></div><div id="footer">
228 <p class="apache">Copyright 2018 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>
229 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.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[//><!--
230 if (typeof(prettyPrint) !== 'undefined') {
231     prettyPrint();
232 }
233 //--><!]]></script>
234 </body></html>