mod_alias - Apache HTTP Server Version 2.4
Modules | Directives | FAQ | Glossary | Sitemap | Report a bug Apache HTTP Server Version 2.4 Apache > HTTP Server > Documentation > Version 2.4 > Modules Apache Module mod_alias Available Languages: en | fr | ja | ko | tr Description: Provides for mapping different parts of the host filesystem in the document tree and for URL redirection Status: Base Module Identifier: alias_module Source File: mod_alias.c Summary The directives contained in this module allow for manipulation and control of URLs as requests arrive at the server.
The Alias and ScriptAlias directives are used to map between URLs and filesystem paths. This allows for content which is not directly under the DocumentRoot served as part of the web document tree. The ScriptAlias directive has the additional effect of marking the target directory as containing only CGI scripts. The Redirect directives are used to instruct clients to make a new request with a different URL. They are often used when a resource has moved to a new location.
When the Alias , ScriptAlias and Redirect directives are used within a <Location> or <LocationMatch> section, expression syntax can be used to manipulate the destination path or URL. mod_alias is designed to handle simple URL manipulation tasks. For more complicated tasks such as manipulating the query string, use the tools provided by mod_rewrite . Topics Order of Processing Directives Alias AliasMatch AliasPreservePath Redirect RedirectMatch RedirectPermanent RedirectRelative RedirectTemp ScriptAlias ScriptAliasMatch Bugfix checklist httpd changelog Known issues Report a bug See also mod_rewrite Mapping URLs to the filesystem Order of Processing ¶ Aliases and Redirects occurring in different contexts are processed like other directives according to standard merging rules .
But when multiple Aliases or Redirects occur in the same context (for example, in the same <VirtualHost> section) they are processed in a particular order. First, all Redirects are processed before Aliases are processed, and therefore a request that matches a Redirect or RedirectMatch will never have Aliases applied. Second, the Aliases and Redirects are processed in the order they appear in the configuration files, with the first match taking precedence.
For this reason, when two or more of these directives apply to the same sub-path, you must list the most specific path first in order for all the directives to have an effect. For example, the following configuration will work as expected: Alias "/foo/bar" "/baz" Alias "/foo" "/gaq" But if the above two directives were reversed in order, the /foo Alias would always match before the /foo/bar Alias , so the latter directive would be ignored.
When the Alias , ScriptAlias and Redirect directives are used within a <Location> or <LocationMatch> section, these directives will take precedence over any globally defined Alias , ScriptAlias and Redirect directives. Alias Directive ¶ Description: Maps URLs to filesystem locations Syntax: Alias [ URL-path ] file-path | directory-path Context: server config, virtual host Status: Base Module: mod_alias The Alias directive allows documents to be stored in the local filesystem other than under the DocumentRoot .
URLs with a (%-decoded) path beginning with URL-path will be mapped to local files beginning with directory-path . The URL-path is case-sensitive, even on case-insensitive file systems. Alias "/image" "/ftp/pub/image" A request for http://example.com/image/foo.gif would cause the server to return the file /ftp/pub/image/foo.gif . Only complete path segments are matched, so the above alias would not match a request for http://example.com/imagefoo.gif .
For more complex matching using regular expressions, see the AliasMatch directive. Note that if you include a trailing / on the URL-path then the server will require a trailing / in order to expand the alias. That is, if you use Alias "/icons/" "/usr/local/apache/icons/" then the URL /icons will not be aliased, as it lacks that trailing /. Likewise, if you omit the slash on the URL-path then you must also omit it from the file-path .
Note that you may need to specify additional <Directory> sections which cover the destination of aliases. Aliasing occurs before <Directory> sections are checked, so only the destination of aliases are affected. (Note however <Location> sections are run through once before aliases are performed, so they will apply.) In particular, if you are creating an Alias to a directory outside of your DocumentRoot , you may need to explicitly permit access to the target directory.
Alias "/image" "/ftp/pub/image" <Directory "/ftp/pub/image"> Require all granted </Directory> Any number slashes in the URL-path parameter matches any number of slashes in the requested URL-path. If the Alias directive is used within a <Location> or <LocationMatch> section the URL-path is omitted, and the file-path is interpreted using expression syntax . This syntax is available in Apache 2.4.19 and later. <Location "/image"> Alias "/ftp/pub/image" </Location> <LocationMatch "/error/(?<NUMBER>[0-9]+)"> Alias "/usr/local/apache/errors/%{env:MATCH_NUMBER}.html" </LocationMatch> Note that when the AliasPreservePath directive is on, the full path is mapped to the destination.
When the directive is off, all URLs are mapped to the single target URL. # /files/foo and /files/bar mapped to /ftp/pub/files/foo and /ftp/pub/files/bar <Location "/files"> AliasPreservePath on Alias "/ftp/pub/files" </Location> # /errors/foo and /errors/bar mapped to /var/www/errors.html <Location "/errors"> AliasPreservePath off Alias "/var/www/errors.html" </Location> AliasMatch Directive ¶ Description: Maps URLs to filesystem locations using regular expressions Syntax: AliasMatch regex file-path | directory-path Context: server config, virtual host Status: Base Module: mod_alias This directive is equivalent to Alias , but makes use of regular expressions , instead of simple prefix matching.
The supplied regular expression is matched against the URL-path, and if it matches, the server will substitute any parenthesized matches into the given string and use it as a filename. For example, to activate the /icons directory, one might use: AliasMatch "^/icons(/|$)(.*)" "/usr/local/apache/icons$1$2" The full range of regular expression power is available. For example, it is possible to construct an alias with case-insensitive matching of the URL-path: AliasMatch "(?i)^/image(.*)" "/ftp/pub/image$1" One subtle difference between Alias and AliasMatch is that Alias will automatically copy any additional part of the URI, past the part that matched, onto the end of the file path on the right side, while AliasMatch will not.
This means that in almost all cases, you will want the regular expression to match the entire request URI from beginning to end, and to use substitution on the right side. In other words, just changing Alias to AliasMatch will not have the same effect. At a minimum, you need to add ^ to the beginning of the regular expression and add (.*)$ to the end, and add $1 to the end of the replacement. For example, suppose you want to replace this with AliasMatch: Alias "/image/" "/ftp/pub/image/" This is NOT equivalent - don't do this!
This will send all requests that have /image/ anywhere in them to /ftp/pub/image/: AliasMatch "/image/" "/ftp/pub/image/" This is what you need to get the same effect: AliasMatch "^/image/(.*)$" "/ftp/pub/image/$1" Of course, there's no point in using AliasMatch where Alias would work. AliasMatch lets you do more complicated things. For example, you could serve different kinds of files from different directories: AliasMatch "^/image/(.*)\.jpg$" "/files/jpg.images/$1.jpg" AliasMatch "^/image/(.*)\.gif$" "/files/gif.images/$1.gif" Multiple leading slashes in the requested URL are discarded by the server before directives from this module compares against the requested URL-path.
AliasPreservePath Directive ¶ Description: Map the full path after the
The rest of this page is on the live site.