Load & activate mod_rewrite
Search inside Apache's httpd.conf
for a line similar to the following and remove the #
at the beginning of the line.
LoadModule mod_rewrite.so
Enable mod_rewrite
RewriteEngine on
Enable mod_rewrite support inside .htaccess files
By default usage of mod_rewrite inside
.htaccess
files is not allowed. To allow usage add the following lines to the webroot Directory block inside your httpd.conf
.
AllowOverride FileInfo
// will also work
AllowOverride All
RewriteCond
mod_rewrite Conditions are constructed as followed:
RewriteCond <test-string> <condition> [<flags>]
Supported conditions
operator | meaning |
< | less than |
> | more than |
= | equal |
-d | check if path is an existing directory |
-f | check if path is an existing file |
-s | check if path is an existing file larger than 0 bytes |
-l | check if path is an symbolic link |
-F | check if path is an existing file and user is authorized to access it |
-U | checks if test string is a valid url and user is authorized to access it |
Flags
flag | meaning |
NC / nocase | case-insensitive matching (default: case-sensitive) |
OR / ornext | combine conditions via logical or (default: logical and) |
Magical values
placeholder | meaning |
$1 .. $9 | buffered values from current rewrite directive |
%1 .. %9 | buffered values from last rewrite condition. |
RewriteRule
mod_rewrite Rewrite rules are constructed as followed:
RewriteRule <original-path-regexp> <rewritten-path> [<flags>]
<original-path-regexp>
is a perl regular expression (PCRE). PCRE delimiter (e.g. /
) are omitted. <original-path-regexp>
can be negated by prefixing it with !
<rewritten-path>
can contain matches from <original-path-regexp>
. Rewrite dependents on context.
Rewrite context
context | rewrite |
main configuration | whole url is rewritten |
inside <Directory> block or .htaccess | only the path from the current directory is rewritten. |
Supported flags
Insinde
RewriteRule
mod_rewrite supports the following flags:
operator | meaning |
chain / C | group rewrite rules in a chain. later rules are executed, only if the previous ones are matching |
cookie=<name>:<value>:<domain>[:<lifetime>[:<path>] / CO= |
set a cookie based on the given data. |
env=<variable>:<value> / E=VAR:VAL | set environment variable variable to value |
forbidden / F | send HTTP status header FORBIDDEN (403) immediately |
gone / G | send HTTP status header GONE (410) |
last / L | stop processing rewrite rules after this one |
operator | meaning |
next / N | abort current rewrite directive and restart rewriting |
nocase / NC | perfom case-insensitive matching |
noescape / NE | disable automatic url-encoding |
nosubreq / NS | |
redirect / R[=301|302|303] | perform a HTTP redirect to destination and send HTTP status header (default: 302) |
passthrough / PT | only needed in complexe scenarios where you use multiple url rewrite engines like mod_rewrite and mod_alias together. |
proxy / P | a requests to the given url is performed by Apache's module mod_proxy |
qsappend / QSA | append parameters to current query string |
skip=[1,2,3,..,n] / S=[1,2,3,..,n] | skip the next n rewrite directives |
type= / T= | specify mime type for request |
Magical values
Insinde
RewriteRule
mod_rewrite supports the magical values:
placeholder | meaning |
$1 .. $9 | buffered values from current rewrite directive |