2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <modulesynopsis metafile="mod_alias.xml.meta">
25 <name>mod_alias</name>
26 <description>Provides for mapping different parts of the host
27 filesystem in the document tree and for URL redirection</description>
29 <sourcefile>mod_alias.c</sourcefile>
30 <identifier>alias_module</identifier>
33 <p>The directives contained in this module allow for manipulation
34 and control of URLs as requests arrive at the server. The
35 <directive module="mod_alias">Alias</directive> and <directive
36 module="mod_alias">ScriptAlias</directive> directives are used to
37 map between URLs and filesystem paths. This allows for content
38 which is not directly under the <directive
39 module="core">DocumentRoot</directive> served as part of the web
40 document tree. The <directive
41 module="mod_alias">ScriptAlias</directive> directive has the
42 additional effect of marking the target directory as containing
45 <p>The <directive module="mod_alias">Redirect</directive>
46 directives are used to instruct clients to make a new request with
47 a different URL. They are often used when a resource has moved to
50 <p>When the <directive module="mod_alias">Alias</directive>,
51 <directive module="mod_alias">ScriptAlias</directive> and
52 <directive module="mod_alias">Redirect</directive> directives are used
53 within a <directive type="section" module="core">Location</directive>
54 or <directive type="section" module="core">LocationMatch</directive>
55 section, <a href="../expr.html">expression syntax</a> can be used
56 to manipulate the destination path or URL.
59 <p><module>mod_alias</module> is designed to handle simple URL
60 manipulation tasks. For more complicated tasks such as
61 manipulating the query string, use the tools provided by
62 <module>mod_rewrite</module>.</p>
66 <seealso><module>mod_rewrite</module></seealso> <seealso><a
67 href="../urlmapping.html">Mapping URLs to the filesystem</a></seealso>
69 <section id="order"><title>Order of Processing</title>
71 <p>Aliases and Redirects occurring in different contexts are processed
72 like other directives according to standard <a
73 href="../sections.html#mergin">merging rules</a>. But when multiple
74 Aliases or Redirects occur in the same context (for example, in the
75 same <directive type="section" module="core">VirtualHost</directive>
76 section) they are processed in a particular order.</p>
78 <p>First, all Redirects are processed before Aliases are processed,
79 and therefore a request that matches a <directive
80 module="mod_alias">Redirect</directive> or <directive
81 module="mod_alias">RedirectMatch</directive> will never have Aliases
82 applied. Second, the Aliases and Redirects are processed in the order
83 they appear in the configuration files, with the first match taking
86 <p>For this reason, when two or more of these directives apply to the
87 same sub-path, you must list the most specific path first in order for
88 all the directives to have an effect. For example, the following
89 configuration will work as expected:</p>
91 <highlight language="config">
92 Alias "/foo/bar" "/baz"
96 <p>But if the above two directives were reversed in order, the
97 <code>/foo</code> <directive module="mod_alias">Alias</directive>
98 would always match before the <code>/foo/bar</code> <directive
99 module="mod_alias">Alias</directive>, so the latter directive would be
102 <p>When the <directive module="mod_alias">Alias</directive>,
103 <directive module="mod_alias">ScriptAlias</directive> and
104 <directive module="mod_alias">Redirect</directive> directives are used
105 within a <directive type="section" module="core">Location</directive>
106 or <directive type="section" module="core">LocationMatch</directive>
107 section, these directives will take precedence over any globally
108 defined <directive module="mod_alias">Alias</directive>,
109 <directive module="mod_alias">ScriptAlias</directive> and
110 <directive module="mod_alias">Redirect</directive> directives.</p>
116 <description>Maps URLs to filesystem locations</description>
117 <syntax>Alias [<var>URL-path</var>]
118 <var>file-path</var>|<var>directory-path</var></syntax>
119 <contextlist><context>server config</context><context>virtual host</context>
120 <context>directory</context>
125 <p>The <directive>Alias</directive> directive allows documents to
126 be stored in the local filesystem other than under the
127 <directive module="core">DocumentRoot</directive>. URLs with a
128 (%-decoded) path beginning with <var>URL-path</var> will be mapped
129 to local files beginning with <var>directory-path</var>. The
130 <var>URL-path</var> is case-sensitive, even on case-insensitive
133 <highlight language="config">
134 Alias "/image" "/ftp/pub/image"
137 <p>A request for <code>http://example.com/image/foo.gif</code> would cause
138 the server to return the file <code>/ftp/pub/image/foo.gif</code>. Only
139 complete path segments are matched, so the above alias would not match a
140 request for <code>http://example.com/imagefoo.gif</code>. For more complex
141 matching using regular expressions, see the <directive module="mod_alias"
142 >AliasMatch</directive> directive.</p>
144 <p>Note that if you include a trailing / on the
145 <var>URL-path</var> then the server will require a trailing / in
146 order to expand the alias. That is, if you use</p>
148 <highlight language="config">
149 Alias "/icons/" "/usr/local/apache/icons/"
152 <p>then the URL <code>/icons</code> will not be aliased, as it lacks
153 that trailing /. Likewise, if you omit the slash on the
154 <var>URL-path</var> then you must also omit it from the
155 <var>file-path</var>.</p>
157 <p>Note that you may need to specify additional <directive
158 type="section" module="core">Directory</directive> sections which
159 cover the <em>destination</em> of aliases. Aliasing occurs before
160 <directive type="section" module="core">Directory</directive> sections
161 are checked, so only the destination of aliases are affected.
162 (Note however <directive type="section" module="core">Location</directive>
163 sections are run through once before aliases are performed, so
164 they will apply.)</p>
166 <p>In particular, if you are creating an <code>Alias</code> to a
167 directory outside of your <directive
168 module="core">DocumentRoot</directive>, you may need to explicitly
169 permit access to the target directory.</p>
171 <highlight language="config">
172 Alias "/image" "/ftp/pub/image"
173 <Directory "/ftp/pub/image">
178 <p>Any number slashes in the <var>URL-path</var> parameter
179 matches any number of slashes in the requested URL-path.</p>
181 <p>If the <directive>Alias</directive> directive is used within a
182 <directive type="section" module="core">Location</directive>
183 or <directive type="section" module="core">LocationMatch</directive>
184 section the URL-path is omitted, and the file-path is interpreted
185 using <a href="../expr.html">expression syntax</a>.<br />
186 This syntax is available in Apache 2.4.19 and later.</p>
188 <highlight language="config">
189 <Location "/image">
190 Alias "/ftp/pub/image"
192 <LocationMatch "/error/(?<NUMBER>[0-9]+)">
193 Alias "/usr/local/apache/errors/%{env:MATCH_NUMBER}.html"
194 </LocationMatch>
201 <name>AliasMatch</name>
202 <description>Maps URLs to filesystem locations using regular
203 expressions</description>
204 <syntax>AliasMatch <var>regex</var>
205 <var>file-path</var>|<var>directory-path</var></syntax>
206 <contextlist><context>server config</context><context>virtual host</context>
210 <p>This directive is equivalent to <directive
211 module="mod_alias">Alias</directive>, but makes use of
212 <glossary ref="regex">regular expressions</glossary>,
213 instead of simple prefix matching. The
214 supplied regular expression is matched against the URL-path, and
215 if it matches, the server will substitute any parenthesized
216 matches into the given string and use it as a filename. For
217 example, to activate the <code>/icons</code> directory, one might
220 <highlight language="config">
221 AliasMatch "^/icons(/|$)(.*)" "/usr/local/apache/icons$1$2"
224 <p>The full range of <glossary ref="regex">regular expression</glossary>
225 power is available. For example,
226 it is possible to construct an alias with case-insensitive
227 matching of the URL-path:</p>
229 <highlight language="config">
230 AliasMatch "(?i)^/image(.*)" "/ftp/pub/image$1"
233 <p>One subtle difference
234 between <directive module="mod_alias">Alias</directive>
235 and <directive module="mod_alias">AliasMatch</directive> is
236 that <directive module="mod_alias">Alias</directive> will
237 automatically copy any additional part of the URI, past the part
238 that matched, onto the end of the file path on the right side,
239 while <directive module="mod_alias">AliasMatch</directive> will
240 not. This means that in almost all cases, you will want the
241 regular expression to match the entire request URI from beginning
242 to end, and to use substitution on the right side.</p>
244 <p>In other words, just changing
245 <directive module="mod_alias">Alias</directive> to
246 <directive module="mod_alias">AliasMatch</directive> will not
247 have the same effect. At a minimum, you need to
248 add <code>^</code> to the beginning of the regular expression
249 and add <code>(.*)$</code> to the end, and add <code>$1</code> to
250 the end of the replacement.</p>
252 <p>For example, suppose you want to replace this with AliasMatch:</p>
254 <highlight language="config">
255 Alias "/image/" "/ftp/pub/image/"
258 <p>This is NOT equivalent - don't do this! This will send all
259 requests that have /image/ anywhere in them to /ftp/pub/image/:</p>
261 <highlight language="config">
262 AliasMatch "/image/" "/ftp/pub/image/"
265 <p>This is what you need to get the same effect:</p>
267 <highlight language="config">
268 AliasMatch "^/image/(.*)$" "/ftp/pub/image/$1"
271 <p>Of course, there's no point in
272 using <directive module="mod_alias">AliasMatch</directive>
273 where <directive module="mod_alias">Alias</directive> would
274 work. <directive module="mod_alias">AliasMatch</directive> lets
275 you do more complicated things. For example, you could
276 serve different kinds of files from different directories:</p>
278 <highlight language="config">
279 AliasMatch "^/image/(.*)\.jpg$" "/files/jpg.images/$1.jpg"
280 AliasMatch "^/image/(.*)\.gif$" "/files/gif.images/$1.gif"
283 <p>Multiple leading slashes in the requested URL are discarded
284 by the server before directives from this module compares
285 against the requested URL-path.
292 <name>Redirect</name>
293 <description>Sends an external redirect asking the client to fetch
294 a different URL</description>
295 <syntax>Redirect [<var>status</var>] [<var>URL-path</var>]
296 <var>URL</var></syntax>
297 <contextlist><context>server config</context><context>virtual host</context>
298 <context>directory</context><context>.htaccess</context></contextlist>
299 <override>FileInfo</override>
302 <p>The <directive>Redirect</directive> directive maps an old URL into a new one by asking
303 the client to refetch the resource at the new location.</p>
305 <p>The old <em>URL-path</em> is a case-sensitive (%-decoded) path
306 beginning with a slash. A relative path is not allowed.</p>
308 <p>The new <em>URL</em> may be either an absolute URL beginning
309 with a scheme and hostname, or a URL-path beginning with a slash.
310 In this latter case the scheme and hostname of the current server will
313 <p>Then any request beginning with <em>URL-path</em> will return a
314 redirect request to the client at the location of the target
315 <em>URL</em>. Additional path information beyond the matched
316 <em>URL-path</em> will be appended to the target URL.</p>
318 <highlight language="config">
319 # Redirect to a URL on a different host
320 Redirect "/service" "http://foo2.example.com/service"
322 # Redirect to a URL on the same host
323 Redirect "/one" "/two"
326 <p>If the client requests <code>http://example.com/service/foo.txt</code>,
327 it will be told to access
328 <code>http://foo2.example.com/service/foo.txt</code>
329 instead. This includes requests with <code>GET</code> parameters, such as
330 <code>http://example.com/service/foo.pl?q=23&a=42</code>,
331 it will be redirected to
332 <code>http://foo2.example.com/service/foo.pl?q=23&a=42</code>.
333 Note that <code>POST</code>s will be discarded.<br />
334 Only complete path segments are matched, so the above
335 example would not match a request for
336 <code>http://example.com/servicefoo.txt</code>. For more complex matching
337 using the <a href="../expr.html">expression syntax</a>, omit the URL-path
338 argument as described below. Alternatively, for matching using regular
339 expressions, see the <directive
340 module="mod_alias">RedirectMatch</directive> directive.</p>
343 <note><title>Note</title>
344 <p><directive>Redirect</directive> directives take precedence over <directive
345 module="mod_alias">Alias</directive> and <directive
346 module="mod_alias">ScriptAlias</directive>
347 directives, irrespective of their ordering in the configuration
348 file. <directive>Redirect</directive> directives inside a Location take
349 precedence over <directive>Redirect</directive> and <directive
350 module="mod_alias">Alias</directive> directives with an <var>URL-path</var>.</p>
353 <p>If no <var>status</var> argument is given, the redirect will
354 be "temporary" (HTTP status 302). This indicates to the client
355 that the resource has moved temporarily. The <var>status</var>
356 argument can be used to return other HTTP status codes:</p>
361 <dd>Returns a permanent redirect status (301) indicating that
362 the resource has moved permanently.</dd>
366 <dd>Returns a temporary redirect status (302). This is the
371 <dd>Returns a "See Other" status (303) indicating that the
372 resource has been replaced.</dd>
376 <dd>Returns a "Gone" status (410) indicating that the
377 resource has been permanently removed. When this status is
378 used the <var>URL</var> argument should be omitted.</dd>
381 <p>Other status codes can be returned by giving the numeric
382 status code as the value of <var>status</var>. If the status is
383 between 300 and 399, the <var>URL</var> argument must be present.
384 If the status is <em>not</em> between 300 and 399, the
385 <var>URL</var> argument must be omitted. The status must be a valid
386 HTTP status code, known to the Apache HTTP Server (see the function
387 <code>send_error_response</code> in http_protocol.c).</p>
389 <highlight language="config">
390 Redirect permanent "/one" "http://example.com/two"
391 Redirect 303 "/three" "http://example.com/other"
394 <p>If the <directive>Redirect</directive> directive is used within a
395 <directive type="section" module="core">Location</directive>
396 or <directive type="section" module="core">LocationMatch</directive>
397 section with the <var>URL-path</var> omitted, then the <var>URL</var> parameter
398 will be interpreted using <a href="../expr.html">expression syntax</a>.<br />
399 This syntax is available in Apache 2.4.19 and later.</p>
401 <highlight language="config">
402 <Location "/one">
403 Redirect permanent "http://example.com/two"
405 <Location "/three">
406 Redirect 303 "http://example.com/other"
408 <LocationMatch "/error/(?<NUMBER>[0-9]+)">
409 Redirect permanent "http://example.com/errors/%{env:MATCH_NUMBER}.html"
410 </LocationMatch>
417 <name>RedirectMatch</name>
418 <description>Sends an external redirect based on a regular expression match
419 of the current URL</description>
420 <syntax>RedirectMatch [<var>status</var>] <var>regex</var>
421 <var>URL</var></syntax>
422 <contextlist><context>server config</context><context>virtual host</context>
423 <context>directory</context><context>.htaccess</context></contextlist>
424 <override>FileInfo</override>
427 <p>This directive is equivalent to <directive
428 module="mod_alias">Redirect</directive>, but makes use of
429 <glossary ref="regex">regular expressions</glossary>,
430 instead of simple prefix matching. The
431 supplied regular expression is matched against the URL-path, and
432 if it matches, the server will substitute any parenthesized
433 matches into the given string and use it as a filename. For
434 example, to redirect all GIF files to like-named JPEG files on
435 another server, one might use:</p>
437 <highlight language="config">
438 RedirectMatch "(.*)\.gif$" "http://other.example.com$1.jpg"
441 <p>The considerations related to the difference between
442 <directive module="mod_alias">Alias</directive> and
443 <directive module="mod_alias">AliasMatch</directive>
444 also apply to the difference between
445 <directive module="mod_alias">Redirect</directive> and
446 <directive module="mod_alias">RedirectMatch</directive>.
447 See <directive module="mod_alias">AliasMatch</directive> for
454 <name>RedirectTemp</name>
455 <description>Sends an external temporary redirect asking the client to fetch
456 a different URL</description>
457 <syntax>RedirectTemp <var>URL-path</var> <var>URL</var></syntax>
458 <contextlist><context>server config</context><context>virtual host</context>
459 <context>directory</context><context>.htaccess</context></contextlist>
460 <override>FileInfo</override>
463 <p>This directive makes the client know that the Redirect is
464 only temporary (status 302). Exactly equivalent to
465 <code>Redirect temp</code>.</p>
470 <name>RedirectPermanent</name>
471 <description>Sends an external permanent redirect asking the client to fetch
472 a different URL</description>
473 <syntax>RedirectPermanent <var>URL-path</var> <var>URL</var></syntax>
474 <contextlist><context>server config</context><context>virtual host</context>
475 <context>directory</context><context>.htaccess</context></contextlist>
476 <override>FileInfo</override>
479 <p>This directive makes the client know that the Redirect is
480 permanent (status 301). Exactly equivalent to <code>Redirect
481 permanent</code>.</p>
486 <name>ScriptAlias</name>
487 <description>Maps a URL to a filesystem location and designates the
488 target as a CGI script</description>
489 <syntax>ScriptAlias [<var>URL-path</var>]
490 <var>file-path</var>|<var>directory-path</var></syntax>
491 <contextlist><context>server config</context><context>virtual host</context>
492 <context>directory</context>
496 <p>The <directive>ScriptAlias</directive> directive has the same
497 behavior as the <directive module="mod_alias">Alias</directive>
498 directive, except that in addition it marks the target directory
499 as containing CGI scripts that will be processed by <module
500 >mod_cgi</module>'s cgi-script handler. URLs with a case-sensitive
501 (%-decoded) path beginning with <var>URL-path</var> will be mapped
502 to scripts beginning with the second argument, which is a full
503 pathname in the local filesystem.</p>
505 <highlight language="config">
506 ScriptAlias "/cgi-bin/" "/web/cgi-bin/"
509 <p>A request for <code>http://example.com/cgi-bin/foo</code> would cause the
510 server to run the script <code>/web/cgi-bin/foo</code>. This configuration
511 is essentially equivalent to:</p>
512 <highlight language="config">
513 Alias "/cgi-bin/" "/web/cgi-bin/"
514 <Location "/cgi-bin">
515 SetHandler cgi-script
520 <p><directive>ScriptAlias</directive> can also be used in conjunction with
521 a script or handler you have. For example:</p>
523 <highlight language="config">
524 ScriptAlias "/cgi-bin/" "/web/cgi-handler.pl"
527 <p>In this scenario all files requested in <code>/cgi-bin/</code> will be
528 handled by the file you have configured, this allows you to use your own custom
529 handler. You may want to use this as a wrapper for CGI so that you can add
530 content, or some other bespoke action.</p>
532 <note type="warning">It is safer to avoid placing CGI scripts under the
533 <directive module="core">DocumentRoot</directive> in order to
534 avoid accidentally revealing their source code if the
535 configuration is ever changed. The
536 <directive>ScriptAlias</directive> makes this easy by mapping a
537 URL and designating CGI scripts at the same time. If you do
538 choose to place your CGI scripts in a directory already
539 accessible from the web, do not use
540 <directive>ScriptAlias</directive>. Instead, use <directive
541 module="core" type="section">Directory</directive>, <directive
542 module="core">SetHandler</directive>, and <directive
543 module="core">Options</directive> as in:
544 <highlight language="config">
545 <Directory "/usr/local/apache2/htdocs/cgi-bin">
546 SetHandler cgi-script
550 This is necessary since multiple <var>URL-paths</var> can map
551 to the same filesystem location, potentially bypassing the
552 <directive>ScriptAlias</directive> and revealing the source code
553 of the CGI scripts if they are not restricted by a
554 <directive module="core">Directory</directive> section.</note>
556 <p>If the <directive>ScriptAlias</directive> directive is used within
557 a <directive type="section" module="core">Location</directive>
558 or <directive type="section" module="core">LocationMatch</directive>
559 section with the URL-path omitted, then the URL parameter will be
560 interpreted using <a href="../expr.html">expression syntax</a>.<br />
561 This syntax is available in Apache 2.4.19 and later.</p>
563 <highlight language="config">
564 <Location "/cgi-bin">
565 ScriptAlias "/web/cgi-bin/"
567 <LocationMatch "/cgi-bin/errors/(?<NUMBER>[0-9]+)">
568 ScriptAlias "/web/cgi-bin/errors/%{env:MATCH_NUMBER}.cgi"
569 </LocationMatch>
573 <seealso><a href="../howto/cgi.html">CGI Tutorial</a></seealso>
577 <name>ScriptAliasMatch</name>
578 <description>Maps a URL to a filesystem location using a regular expression
579 and designates the target as a CGI script</description>
580 <syntax>ScriptAliasMatch <var>regex</var>
581 <var>file-path</var>|<var>directory-path</var></syntax>
582 <contextlist><context>server config</context><context>virtual host</context>
586 <p>This directive is equivalent to <directive module="mod_alias"
587 >ScriptAlias</directive>, but makes use of
588 <glossary ref="regex">regular expressions</glossary>,
589 instead of simple prefix matching. The
590 supplied regular expression is matched against the URL-path,
591 and if it matches, the server will substitute any parenthesized
592 matches into the given string and use it as a filename. For
593 example, to activate the standard <code>/cgi-bin</code>, one
596 <highlight language="config">
597 ScriptAliasMatch "^/cgi-bin(.*)" "/usr/local/apache/cgi-bin$1"
600 <p>As for AliasMatch, the full range of <glossary ref="rexex">regular
601 expression</glossary> power is available.
602 For example, it is possible to construct an alias with case-insensitive
603 matching of the URL-path:</p>
605 <highlight language="config">
606 ScriptAliasMatch "(?i)^/cgi-bin(.*)" "/usr/local/apache/cgi-bin$1"
609 <p>The considerations related to the difference between
610 <directive module="mod_alias">Alias</directive> and
611 <directive module="mod_alias">AliasMatch</directive>
612 also apply to the difference between
613 <directive module="mod_alias">ScriptAlias</directive> and
614 <directive module="mod_alias">ScriptAliasMatch</directive>.
615 See <directive module="mod_alias">AliasMatch</directive> for