mod_rewrite is a Apache webserver module for flexible url rewriting. It is part of the standard Apache installation on all platforms.
Basic
Load & activate mod_rewrite
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.
Rewrite Condition
mod_rewrite Conditions are constructed as followed:
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
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.
Rewrite Rule
<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 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=::[:[:] / CO=
set a cookie based on the given data.
env=: / 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
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
Server Variables
Server variables are handy for writing complex mod_rewrite rules sets. The following are available inside mod_rewrite.
Request
HTTP Headers
Time
Misc
Redirect
Files and directory
Redirect (sub)domains
Url mapping
Handling query strings
Security
Authorization
Deny access by IP
In most cases using Allow <IP> and Deny <IP> is better. However you can also control access by IP via mod_rewrite.