A small yet handy cheat sheet for mod_rewrite. The most used items to build a mod_rewrite pattern.

Despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo.

Layout

RewriteRule Pattern Substituion [Flag(s)]

Example of rewriting oldfiles to new files:

RewriteRule &/oldfile\.html$ /newfile.html

Operators

. Every char
< > = Compare
\ Escape a char
.+ One more, more chars
.* No chars or multiple chars
^ Start
$ End
(…) Group
a|b .A or B
(a|b) .A or B group
a{5} Exact 5 times a
a{1,5} Between 1 and 5 times a
[a-z]* Match chars
-d Directory
-f File
-l Symbolic link

Flags

[NC] No case sensitive
[OR] Allows multiple lines
[R=#] Redirect where # is number: 404.
[L] Terminate routine

RewriteCond variables

%{HTTP_ACCEPT} Media types accepted by the client, ?text/plain?
%{HTTP_COOKIE} Cookies set for the client.
%{HTTP_HOST} Domain name of the virtual host queried.
%{HTTP_REFERER} Page with a link to this page (can be omitted).
%{HTTP_USER_AGENT} Client, such as ?Mozilla/4.0?
%{QUERY_STRING} Query string transferred by a GET form.
%{REMOTE_ADDR} Client IP address.
%{REMOTE_HOST} Domain name of client.
%{REMOTE_USER} User name of the client.
%{REQUEST_URI} The URI requested by the client.
%{REQUEST_FILENAME} The corresponding file on the local file system.
%{SERVER_ADDR} Server IP