This document supplements the
mod_rewrite should be considered a last resort, when other alternatives are found wanting. Using it when there are simpler alternatives leads to configurations which are confusing, fragile, and hard to maintain. Understanding what other alternatives are available is a very important step towards mod_rewrite mastery.
Note that many of these examples won't work unchanged in your particular server configuration, so it's important that you understand them, rather than merely cutting and pasting the examples into your configuration.
The most common situation in which
RedirectMatch
allows you to include a regular expression in your redirection criteria,
providing many of the benefits of using RewriteRule
.
A common use for RewriteRule
is to redirect an entire
class of URLs. For example, all URLs in the /one
directory
must be redirected to http://one.example.com/
, or perhaps
all http
requests must be redirected to
https
.
These situations are better handled by the Redirect
directive. Remember that Redirect
preserves path
information. That is to say, a redirect for a URL /one
will
also redirect all URLs under that, such as /one/two.html
and /one/three/four.html
.
To redirect URLs under /one
to
http://one.example.com
, do the following:
To redirect one hostname to another, for example
example.com
to www.example.com
, see the
Canonical Hostnames
recipe.
To redirect http
URLs to https
, do the
following:
The use of RewriteRule
to perform this task may be
appropriate if there are other RewriteRule
directives in
the same scope. This is because, when there are Redirect
and RewriteRule
directives in the same scope, the
RewriteRule
directives will run first, regardless of the
order of appearance in the configuration file.
In the case of the http-to-https redirection, the use of
RewriteRule
would be appropriate if you don't have access
to the main server configuration file, and are obliged to perform this
task in a .htaccess
file instead.
The mod_rewrite
,
Alias
is the preferred method, for reasons of simplicity
and performance.
The use of mod_rewrite
to perform this mapping may be
appropriate when you do not have access to the server configuration
files. Alias may only be used in server or virtualhost context, and not
in a .htaccess
file.
Symbolic links would be another way to accomplish the same thing, if
you have Options FollowSymLinks
enabled on your
server.
Although it is possible to handle virtual hosts
with mod_rewrite, it is seldom the right way. Creating individual
<VirtualHost> blocks is almost always the right way to go. In the
event that you have an enormous number of virtual hosts, consider using
Modules such as
Using .htaccess
files.
See the virtual hosts with mod_rewrite document for more details on how you might accomplish this if it still seems like the right approach.
RewriteRule
provides the [P] flag to pass rewritten URIs through
However, in many cases, when there is no actual pattern matching
needed, as in the example shown above, the
Note that whether you use
You may need to use RewriteRule
instead when there are
other RewriteRule
s in effect in the same scope, as a
RewriteRule
will usually take effect before a
ProxyPass
, and so may preempt what you're trying to
accomplish.
Consider, for example, the common scenario where
www.example.com
instead of
example.com
. This can be done using the
This technique can be used to take actions based on any request
header, response header, or environment variable, replacing
See especially the expression evaluation documentation for a overview of what types of expressions you can use in <If> sections, and in certain other directives.