Directives provided by mod-rewrite in Apache 2.4
Syntax#
- RewriteBase URL-path
- RewriteCond TestString CondPattern
- RewriteEngine on|off
- RewriteMap MapName MapType:MapSource
- RewriteOptions Options
- RewriteRule Pattern Substitution [flags]
List of directives available in Apache 2.4
Apache 2.4 provides the following 6 directives via the mod_rewrite
module:
- RewriteBase
- RewriteCond
- RewriteEngine
- RewriteMap
- RewriteOptions
- RewriteRule
The following directives, available previously in Apache 2.2 have been removed:
- RewriteLock
- RewriteLog
- RewriteLogLevel
All the directives (with the exception of RewriteMap
) defined by mod_rewrite
can be allowed to override on a per-directory .htaccess
through the AllowOverride FileInfo
.
Directive | Context | Description |
---|---|---|
RewriteBase | directory, .htaccess | Sets base URL for per directory rewrite |
RewriteCond | Everywhere | Defines conditions under which the rewrite action will occur |
RewriteEngine | Everywhere | Sets status of rewrite engine |
RewriteMap | server config, virtual host | Defines a key lookup function |
RewriteOptions | Everywhere | Sets special options for rewrite engine |
RewriteRule | Everywhere | Defines specific rules for rewrite engine |
The context Everwhere
means that the directive can be defined in any of the following four locations:
- server config
- virtual host config
- directory context
- .htaccess file
The RewriteLog
and RewriteLogLevel
directives have been merged with the global LogLevel
directive and would be used as:
LogLevel rewrite:<level>
where <level>
is a value from trace8
(least significant) to emerg
(most significant). This list is available here.
RewriteBase and RewriteEngine
Directive | Default | Context | Description |
---|---|---|---|
RewriteBase | None | Directory, .htaccess | Sets base URL for per directory rewrite |
RewriteEngine | off | everywhere | Enable or disable runtime rewrite engine |
RewriteBase
The directive specifies URL prefix to be used for substituting relative paths.
RewriteEngine
The directive, if set to off
, will perform no runtime rewrite processing. These rules are not inherited by the virtual hosts (from server config), and will have to be defined individually.
RewriteMap
The directive defines a function which’ll lookup a key in the defined map and substitutes the lookup with its replacement from the map.
The mapping function is defined with the RewriteMap
directive itself as follows:
RewriteMap MAPNAME Type:Source
and can be referenced in any of the RewriteCond
or RewriteRule
directives to act as a substitution guide as follows:
${ MAPNAME : KEY | DEFAULT }
The following are valid values for Type
in the map definition:
int
- allowstoupper
,tolower
,escape
andunescape
onlytxt
- searches a text filedbd
- looks up in a database using SQLSELECT
statementrnd
- random lookups from text filedbm
- similar totxt
, except that thehttxt2dbm
needs to convert the data to hashesfastdbd
- looks up in a database using SQLSELECT
statement with caching