Guide to Apache mod_rewrite
The great thing about mod_rewrite is it gives you all the
configurability and flexibility of Sendmail. The downside to
mod_rewrite is that it gives you all the configurability and
flexibility of Sendmail.
-- Brian Behlendorf
Despite the tons of examples and docs, mod_rewrite is voodoo.
Damned cool voodoo, but still voodoo.
-- Brian Moore
mod_rewrite provides a way to modify incoming
URL requests, dynamically, based on regular
expression rules. This allows you to map arbitrary URLs onto
your internal URL structure in any way you like.
This guide supplements the
reference manual with annotated examples, conceptual
explanations, and practical advice. It is organized as follows:
- Introduction
- Core concepts: regular expression syntax, RewriteRule and
RewriteCond basics, and how mod_rewrite fits into the request
processing lifecycle.
- Per-directory Rewrites
- The key differences between using rewrite rules in server
configuration versus per-directory
context, including path stripping, RewriteBase, and the looping behavior
of the [L] flag.
- RewriteRule Flags
- A complete reference for all flags that can modify the behavior of
a RewriteRule, with examples for each.
- Using RewriteMap
- How to use external lookup sources — text files, DBM databases,
SQL queries, and internal functions — to drive your rewrite
rules.
- Redirection and Remapping
- Recipes for common tasks: HTTPS redirection, canonical hostnames,
trailing slash normalization, front-controller routing, and more.
- Dynamic Virtual Hosts
- Using mod_rewrite to dynamically map hostnames to document roots
without individual VirtualHost blocks.
- When NOT to use mod_rewrite
- Many common tasks are better accomplished with simpler directives.
This document shows the alternatives and when to prefer them.
- Technical Details
- How mod_rewrite hooks into the Apache request processing phases,
and the order in which rules and conditions are evaluated.