Home

RewriteRule

RewriteRule is a directive of the Apache HTTP Server's mod_rewrite module that defines rules for rewriting requested URLs. It enables server-side URL transformation, supporting internal rewrites, redirects, and complex routing strategies used by many web applications to present clean or stable URLs.

Syntax and usage: A rule has the form RewriteRule Pattern Substitution [Flags]. Pattern is a regular expression

Context and matching rules: Rules may be placed in server or virtual host configuration, directory sections,

Processing and effects: When a request is processed, mod_rewrite evaluates rules in order. If a rule matches,

Common use cases include creating clean URLs for content management systems, implementing front controllers for web

Security and maintenance: Misconfigured rules can cause redirect loops or performance issues. Testing configurations and documenting

matched
against
the
URL
path
being
processed.
Substitution
is
the
target
URL
or
path
to
serve
or
redirect
to.
Flags
modify
behavior,
including
L
for
last
rule,
R
for
external
redirects,
and
QSA
to
preserve
the
original
query
string.
or
.htaccess
files.
In
per-directory
context,
the
Pattern
matches
the
URL-path
relative
to
the
directory,
and
the
initial
path
prefix
is
stripped
before
evaluation.
RewriteCond
directives
can
be
used
to
enable
a
RewriteRule
only
when
a
condition
is
met.
its
Substitution
is
applied.
The
request
may
be
rewritten
internally
or
redirected
to
a
new
URL
depending
on
flags.
The
L
flag
halts
further
rule
processing
for
the
current
pass.
Multiple
passes
can
occur
if
a
substitution
changes
the
URL.
frameworks,
canonical
redirects,
and
basic
access
control.
Example:
RewriteRule
^articles/([0-9]+)/(.*)$
article.php?id=$1&title=$2
[L,QSA]
rules
help
maintain
predictable
behavior;
prefer
explicit
patterns,
and
avoid
broad
wildcards
that
can
affect
large
portions
of
the
site.
See
also
mod_rewrite
and
RewriteCond.