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