Summary
This module allows you to control URL access to your HTTP Server.
For example, to prevent a particular user agent called Web crawler from accessing any pages on the server. To do this, include the following directives in your configuration:
RewriteEngine on RewriteCond %{HTTP_USER_AGENT} ^Webcrawler RewriteRule ^.*$ - [F,L]
The first line enables the rewrite engine. The second line provides a test that returns true if the HTTP_USER_AGENT string starts with the letters Web crawler. If the second line is true, then the third line takes any URL string and returns a forbidden message to the client.
Directives
Module: mod_rewrite | |
Syntax: RewriteBase Base_URL | |
Default: RewriteBase physical directory path | |
Context: Directory, but not Location, .htaccess | |
Override: FileInfo | |
Origin: Apache | |
Example: RewriteBase /xyz |
The RewriteBase directive explicitly sets the base URL for per-directory rewrites. As you will see below, RewriteRule can be used in per-directory config files (.htaccess). There it will act locally (for example, the local directory prefix is stripped at this stage of processing and your rewriting rules act only on the remainder). At the end it is automatically added back to the path.
When a substitution occurs for a new URL, this module has to re-inject the URL into the processing server. To be able to do this it needs to know what the corresponding URL-prefix or URL-base is. By default this prefix is the corresponding filepath itself. At most, Web sites URLs are not directly related to physical filename paths, so this assumption is usually incorrect. In this case, you have to use the RewriteBase directive to specify the correct URL-prefix.
Assume the following per-directory configuration file (/abc/def is the physical path of /xyz, and the server has the 'Alias /xyz /ABC/def' established).
RewriteEngine On RewriteBase /xyz RewriteRule ^old\.html$ new.html
In the above example, a request to /xyz/old.html is correctly rewritten to the physical file /ABC/def/new.html.
Module: mod_rewrite | |
Syntax: RewriteCond TestString CondPattern [flags] | |
Default: none | |
Context: server config, virtual host, directory, .htaccess | |
Override: FileInfo | |
Origin: Apache | |
Example: RewriteCond %{HTTP_USER_AGENT} ^Mozilla.* |
The RewriteCond directive defines a rule condition. Precede a RewriteRule directive with one or more RewriteCond directives. The following rewriting rule is only used if its pattern matches the current state of the URI and if these additional conditions apply.
CondPattern is a standard Extended Regular Expression with some additions:
RewriteCond %{REMOTE_HOST} ^host1.* [OR] RewriteCond %{REMOTE_HOST} ^host2.* [OR] RewriteCond %{REMOTE_HOST} ^host3.* RewriteRule ...some special stuff for any of these hosts...Without this flag you would have to write the cond/rule three times.
To rewrite the Homepage of a site according to the ``User-Agent:'' header of the request, you can use the following:
RewriteCond %{HTTP_USER_AGENT} ^Mozilla.* RewriteRule ^/$ /homepage.max.html [L] RewriteCond %{HTTP_USER_AGENT} ^Lynx.* RewriteRule ^/$ /homepage.min.html [L] RewriteRule ^/$ /homepage.std.html [L]
If you use Netscape Navigator as your browser (which identifies itself as 'Mozilla'), then you get the max homepage, which includes Frames and so on. If you use the Lynx browser (which is Terminal-based), then you get the min homepage, which contains no images, no tables, and so on. If you use any other browser you get the standard homepage.
Module: mod_rewrite | |
Syntax: RewriteEngine on | off | |
Default: RewriteEngine off | |
Context: server config, virtual host, directory, .htaccess | |
Override: FileInfo | |
Origin: Apache | |
Example: RewriteEngine on |
The RewriteEngine directive enables or disables the runtime rewriting engine. You can use this directive to disable the module instead of commenting out all the RewriteRule directives.
Module: mod_rewrite | |
Syntax: RewriteLog filename | |
Default: none | |
Context: server config, virtual host | |
Override: none | |
Origin: Apache | |
Example: RewriteLog "/usr/local/var/apache/logs/rewrite.log" |
The RewriteLog directive sets the name of the file to which the server logs any rewriting actions it performs. The directive should occur only once per server configuration.
RewriteLog "/usr/local/var/apache/logs/rewrite.log"
Module: mod_rewrite | |
Syntax: RewriteLogLevel Level | |
Default: RewriteLogLevel 0 | |
Context: server config, virtual host | |
Override: none | |
Origin: Apache | |
Example: RewriteLogLevel 3 |
The RewriteLogLevel directive sets the level of the rewriting logfile.
RewriteLogLevel 3To disable the logging of rewriting actions simply set Level to 0. This disables all rewrite action logs.
Module: mod_rewrite | |
Syntax: RewriteMap MapName MapType:MapSource | |
Default: none | |
Context: server config, virtual host | |
Override: none | |
Origin: Apache | |
Example: RewriteMap servers rnd:/path/to/file/map.txt |
The RewriteMap directive defines a Rewriting Map that can be used inside rule substitution strings by the mapping-functions to insert or substitute fields through a key lookup. The source of this lookup can be of various types.
${ MapName : LookupKey } ${ MapName : LookupKey | DefaultValue }
When such a construct occurs the map MapName is consulted and the key LookupKey is looked-up. If the key is found, the map-function construct is substituted by SubstValue. If the key is not found then it is substituted by DefaultValue or by the empty string if no DefaultValue was specified. The following combinations for MapType and MapSource can be used:
This is the standard rewriting map feature where the MapSource is a plain text file containing either blank lines, comment lines (starting with a '#' character) or pairs like the following (one per line): MatchingKey SubstituionValue.
File example:
## ## map.txt -- rewriting map ## Ralf.B.Jones rbj # Operator Mr.Joe.Average joe # Mr. Average
Directive example:
RewriteMap real-to-user txt:/path/to/file/map.txt
This is identical to the Standard Plain Text variant above but with a special post-processing feature. After looking up a value it is parsed according to the contained horizontal bar ( | ) characters which mean "or". In other words, the horizontal bars indicate a set of alternatives from which the actual returned value is randomly chosen. This feature was designed for load balancing in a reverse proxy situation where the looked up values are server names.
File example:
## ## map.txt -- rewriting map ## static www1|www2|www3|www4 dynamic www5|www6
Directive example:
RewriteMap servers rnd:/path/to/file/map.txt
The following internal functions are valid:
The RewriteMap directive can occur more than once. For each mapping function use one RewriteMap directive to declare its rewriting mapfile. While you cannot declare a map in a per-directory context, it is possible to use this map in a per-directory context.
Module: mod_rewrite | |
Syntax: RewriteOptions Option | |
Default: none | |
Context: server config, virtual host, directory, .htaccess | |
Override: FileInfo | |
Origin: Apache | |
Example: RewriteOptions inherit |
The RewriteOptions directive sets some special options for the current per-server or per-directory configuration.
- Parameter: Option
- The Option parameter strings can be one of the following:
Module: mod_rewrite | |
Syntax: RewriteRule pattern substitution [flags] | |
Default: none | |
Context: server config, virtual host, directory, .htaccess | |
Override: FileInfo | |
Origin: Apache | |
Example: RewriteRule ^/ABC(.*) /def$1 [PT] |
The RewriteRule directive is the real rewriting workhorse. The directive can occur more than once. Each directive then defines one single rewriting rule. The definition order of these rules is important, because this order is used when applying the rules at run-time.
Remember, an unconditional external redirect to your own server will not work with the prefix http://thishost because of this feature. To achieve such a self-redirect, you have to use the R-flag (see below).
RewriteRule ^/ABC(.*) /def$1 [PT] Alias /def /ghi
If you omit the PT flag then mod_rewrite will do its job fine, for example, it rewrites uri=/ABC/... to filename=/def/... as a full API-compliant URI-to-filename translator should do. Then mod_alias comes and tries to do a URI-to-filename transition which will not work.
Possible substitution combinations and meanings:
Given rule | Resulting substitution |
---|---|
^/somepath(.*) otherpath$1 | not supported |
^/somepath(.*) otherpath$1 [R] | not supported |
^/somepath(.*) otherpath$1 [P] | not supported |
^/somepath(.*) /otherpath$1 | /otherpath/pathinfo |
^/somepath(.*) /otherpath$1 [R] | http://thishost/otherpath/pathinfo via external redirection |
^/somepath(.*) /otherpath$1 [P] | not supported |
^/somepath(.*) http://thishost/otherpath$1 | /otherpath/pathinfo |
^/somepath(.*) http://thishost/otherpath$1 [R] | http://thishost/otherpath/pathinfo via external redirection |
^/somepath(.*) http://thishost/otherpath$1 [P] | not supported |
^/somepath(.*) http://otherhost/otherpath$1 | http://otherhost/otherpath/pathinfo via external redirection |
^/somepath(.*) http://otherhost/otherpath$1 [R] | ^/somepath(.*) http://otherhost/otherpath$1 [R] |
^/somepath(.*) http://otherhost/otherpath$1 [P] | http://otherhost/otherpath/pathinfo via internal proxy |
^localpath(.*) otherpath$1 | /somepath/otherpath/pathinfo |
^localpath(.*) otherpath$1 [R] | http://thishost/somepath/otherpath/pathinfo via external redirection |
^localpath(.*) otherpath$1 [P] | not supported |
^localpath(.*) /otherpath$1 | /otherpath/pathinfo |
^localpath(.*) /otherpath$1 [R] | http://thishost/otherpath/pathinfo via external redirection |
^localpath(.*) /otherpath$1 [P] | not supported |
^localpath(.*) http://thishost/otherpath$1 | /otherpath/pathinfo |
^localpath(.*) http://thishost/otherpath$1 [R] | http://thishost/otherpath/pathinfo via external redirection |
^localpath(.*) http://thishost/otherpath$1 [P] | not supported |
^localpath(.*) http://otherhost/otherpath$1 | http://otherhost/otherpath/pathinfo via external redirection |
^localpath(.*) http://otherhost/otherpath$1 [R] | http://otherhost/otherpath/pathinfo via external redirection (the [R] flag is redundant) |
^localpath(.*) http://otherhost/otherpath$1 [P] | http://otherhost/otherpath/pathinfo via internal proxy |
If you wanted to rewrite URLs of the form / Language /~ Realname /.../ File into /u/ Username /.../ File . Language, you would take the rewrite mapfile from above and save it under /path/to/file/map.txt. Then we only have to add the following lines to HTTP Server configuration file:
RewriteLog /path/to/file/rewrite.log RewriteMap real-to-user txt:/path/to/file/map.txt RewriteRule ^/([^/]+)/~([^/]+)/(.*)$ /u/${real-to-user:$2|nobody}/$3.$1