|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
|
|
Packit |
90a5c9 |
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type" />
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
|
Packit |
90a5c9 |
This file is generated from xml source: DO NOT EDIT
|
|
Packit |
90a5c9 |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
|
Packit |
90a5c9 |
-->
|
|
Packit |
90a5c9 |
<title>When not to use mod_rewrite - Apache HTTP Server Version 2.4</title>
|
|
Packit |
90a5c9 |
<link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
|
|
Packit |
90a5c9 |
<link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
|
|
Packit |
90a5c9 |
<link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="../style/css/prettify.css" />
|
|
Packit |
90a5c9 |
<script src="../style/scripts/prettify.min.js" type="text/javascript">
|
|
Packit |
90a5c9 |
</script>
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
<link href="../images/favicon.ico" rel="shortcut icon" /></head>
|
|
Packit |
90a5c9 |
<body id="manual-page">
|
|
Packit |
90a5c9 |
Modules | Directives | FAQ | Glossary | Sitemap
|
|
Packit |
90a5c9 |
Apache HTTP Server Version 2.4
|
|
Packit |
90a5c9 |
![](../images/feather.png)
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
Apache > HTTP Server > Documentation > Version 2.4 > RewriteWhen not to use mod_rewrite
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
Available Languages: en |
|
|
Packit |
90a5c9 |
fr
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
This document supplements the mod_rewrite
|
|
Packit |
90a5c9 |
reference documentation. It describes
|
|
Packit |
90a5c9 |
perhaps one of the most important concepts about mod_rewrite - namely,
|
|
Packit |
90a5c9 |
when to avoid using it.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
mod_rewrite should be considered a last resort, when other
|
|
Packit |
90a5c9 |
alternatives are found wanting. Using it when there are simpler
|
|
Packit |
90a5c9 |
alternatives leads to configurations which are confusing, fragile, and
|
|
Packit |
90a5c9 |
hard to maintain. Understanding what other alternatives are available is
|
|
Packit |
90a5c9 |
a very important step towards mod_rewrite mastery.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
Note that many of these examples won't work unchanged in your
|
|
Packit |
90a5c9 |
particular server configuration, so it's important that you understand
|
|
Packit |
90a5c9 |
them, rather than merely cutting and pasting the examples into your
|
|
Packit |
90a5c9 |
configuration.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
The most common situation in which mod_rewrite is
|
|
Packit |
90a5c9 |
the right tool is when the very best solution requires access to the
|
|
Packit |
90a5c9 |
server configuration files, and you don't have that access. Some
|
|
Packit |
90a5c9 |
configuration directives are only available in the server configuration
|
|
Packit |
90a5c9 |
file. So if you are in a hosting situation where you only have .htaccess
|
|
Packit |
90a5c9 |
files to work with, you may need to resort to
|
|
Packit |
90a5c9 |
mod_rewrite .
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
URL Aliasing
|
|
Packit |
90a5c9 |
Virtual Hosting
|
|
Packit |
90a5c9 |
Simple Proxying
|
|
Packit |
90a5c9 |
Environment Variable Testing
|
|
Packit |
90a5c9 |
See also
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
mod_alias provides the Redirect and RedirectMatch directives, which provide a
|
|
Packit |
90a5c9 |
means to redirect one URL to another. This kind of simple redirection of
|
|
Packit |
90a5c9 |
one URL, or a class of URLs, to somewhere else, should be accomplished
|
|
Packit |
90a5c9 |
using these directives rather than RewriteRule . RedirectMatch
|
|
Packit |
90a5c9 |
allows you to include a regular expression in your redirection criteria,
|
|
Packit |
90a5c9 |
providing many of the benefits of using RewriteRule .
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
A common use for RewriteRule is to redirect an entire
|
|
Packit |
90a5c9 |
class of URLs. For example, all URLs in the /one directory
|
|
Packit |
90a5c9 |
must be redirected to http://one.example.com/ , or perhaps
|
|
Packit |
90a5c9 |
all http requests must be redirected to
|
|
Packit |
90a5c9 |
https .
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
These situations are better handled by the Redirect
|
|
Packit |
90a5c9 |
directive. Remember that Redirect preserves path
|
|
Packit |
90a5c9 |
information. That is to say, a redirect for a URL /one will
|
|
Packit |
90a5c9 |
also redirect all URLs under that, such as /one/two.html
|
|
Packit |
90a5c9 |
and /one/three/four.html .
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
To redirect URLs under /one to
|
|
Packit |
90a5c9 |
http://one.example.com , do the following:
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
Redirect "/one/" "http://one.example.com/"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
To redirect one hostname to another, for example
|
|
Packit |
90a5c9 |
example.com to www.example.com , see the
|
|
Packit |
90a5c9 |
Canonical Hostnames
|
|
Packit |
90a5c9 |
recipe.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
To redirect http URLs to https , do the
|
|
Packit |
90a5c9 |
following:
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
<VirtualHost *:80>
|
|
Packit |
90a5c9 |
ServerName www.example.com
|
|
Packit |
90a5c9 |
Redirect "/" "https://www.example.com/"
|
|
Packit |
90a5c9 |
</VirtualHost>
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
<VirtualHost *:443>
|
|
Packit |
90a5c9 |
ServerName www.example.com
|
|
Packit |
90a5c9 |
# ... SSL configuration goes here
|
|
Packit |
90a5c9 |
</VirtualHost>
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
The use of RewriteRule to perform this task may be
|
|
Packit |
90a5c9 |
appropriate if there are other RewriteRule directives in
|
|
Packit |
90a5c9 |
the same scope. This is because, when there are Redirect
|
|
Packit |
90a5c9 |
and RewriteRule directives in the same scope, the
|
|
Packit |
90a5c9 |
RewriteRule directives will run first, regardless of the
|
|
Packit |
90a5c9 |
order of appearance in the configuration file.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
In the case of the http-to-https redirection, the use of
|
|
Packit |
90a5c9 |
RewriteRule would be appropriate if you don't have access
|
|
Packit |
90a5c9 |
to the main server configuration file, and are obliged to perform this
|
|
Packit |
90a5c9 |
task in a .htaccess file instead.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
The Alias directive
|
|
Packit |
90a5c9 |
provides mapping from a URI to a directory - usually a directory outside
|
|
Packit |
90a5c9 |
of your DocumentRoot . Although it
|
|
Packit |
90a5c9 |
is possible to perform this mapping with mod_rewrite ,
|
|
Packit |
90a5c9 |
Alias is the preferred method, for
|
|
Packit |
90a5c9 |
reasons of simplicity and performance.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
Using AliasAlias "/cats" "/var/www/virtualhosts/felines/htdocs"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
The use of mod_rewrite to perform this mapping may be
|
|
Packit |
90a5c9 |
appropriate when you do not have access to the server configuration
|
|
Packit |
90a5c9 |
files. Alias may only be used in server or virtualhost context, and not
|
|
Packit |
90a5c9 |
in a .htaccess file.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
Symbolic links would be another way to accomplish the same thing, if
|
|
Packit |
90a5c9 |
you have Options FollowSymLinks enabled on your
|
|
Packit |
90a5c9 |
server.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
Although it is possible to handle virtual hosts
|
|
Packit |
90a5c9 |
with mod_rewrite, it is seldom the right way. Creating individual
|
|
Packit |
90a5c9 |
<VirtualHost> blocks is
|
|
Packit |
90a5c9 |
almost always the right way to go. In the
|
|
Packit |
90a5c9 |
event that you have an enormous number of virtual hosts, consider using
|
|
Packit |
90a5c9 |
mod_vhost_alias to create these hosts automatically.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
Modules such as mod_macro are
|
|
Packit |
90a5c9 |
also useful for creating a large number of virtual hosts dynamically.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
Using mod_rewrite for vitualhost creation may be
|
|
Packit |
90a5c9 |
appropriate if you are using a hosting service that does not provide
|
|
Packit |
90a5c9 |
you access to the server configuration files, and you are therefore
|
|
Packit |
90a5c9 |
restricted to configuration using .htaccess files.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
See the virtual hosts with mod_rewrite
|
|
Packit |
90a5c9 |
document for more details on how you might accomplish this if it still
|
|
Packit |
90a5c9 |
seems like the right approach.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
RewriteRule provides the [P] flag to pass rewritten URIs through
|
|
Packit |
90a5c9 |
mod_proxy .
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
RewriteRule "^/?images(.*)" "http://imageserver.local/images$1" [P]
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
However, in many cases, when there is no actual pattern matching
|
|
Packit |
90a5c9 |
needed, as in the example shown above, the ProxyPass directive is a better choice.
|
|
Packit |
90a5c9 |
The example here could be rendered as:
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ProxyPass "/images/" "http://imageserver.local/images/"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
Note that whether you use RewriteRule or ProxyPass , you'll still need to use the
|
|
Packit |
90a5c9 |
ProxyPassReverse directive to
|
|
Packit |
90a5c9 |
catch redirects issued from the back-end server:
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ProxyPassReverse "/images/" "http://imageserver.local/images/"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
You may need to use RewriteRule instead when there are
|
|
Packit |
90a5c9 |
other RewriteRule s in effect in the same scope, as a
|
|
Packit |
90a5c9 |
RewriteRule will usually take effect before a
|
|
Packit |
90a5c9 |
ProxyPass , and so may preempt what you're trying to
|
|
Packit |
90a5c9 |
accomplish.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
mod_rewrite is frequently used to take a particular
|
|
Packit |
90a5c9 |
action based on the presence or absence of a particular environment
|
|
Packit |
90a5c9 |
variable or request header. This can be done more efficiently using the
|
|
Packit |
90a5c9 |
<If> .
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
Consider, for example, the common scenario where
|
|
Packit |
90a5c9 |
RewriteRule is used to enforce a canonical
|
|
Packit |
90a5c9 |
hostname, such as www.example.com instead of
|
|
Packit |
90a5c9 |
example.com . This can be done using the <If> directive, as shown here:
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
<If "req('Host') != 'www.example.com'">
|
|
Packit |
90a5c9 |
Redirect "/" "http://www.example.com/"
|
|
Packit |
90a5c9 |
</If>
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
This technique can be used to take actions based on any request
|
|
Packit |
90a5c9 |
header, response header, or environment variable, replacing
|
|
Packit |
90a5c9 |
mod_rewrite in many common scenarios.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
See especially the expression evaluation
|
|
Packit |
90a5c9 |
documentation for a overview of what types of expressions you can
|
|
Packit |
90a5c9 |
use in <If> sections,
|
|
Packit |
90a5c9 |
and in certain other directives.
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
Available Languages: en |
|
|
Packit |
90a5c9 |
fr
|
|
Packit |
90a5c9 |
Notice:This is not a Q&A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our mailing lists.
|
|
Packit |
90a5c9 |
<script type="text/javascript">
|
|
Packit |
90a5c9 |
var comments_shortname = 'httpd';
|
|
Packit |
90a5c9 |
var comments_identifier = 'http://httpd.apache.org/docs/2.4/rewrite/avoid.html';
|
|
Packit |
90a5c9 |
(function(w, d) {
|
|
Packit |
90a5c9 |
if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
|
|
Packit |
90a5c9 |
d.write('
|
|
Packit |
90a5c9 |
var s = d.createElement('script');
|
|
Packit |
90a5c9 |
s.type = 'text/javascript';
|
|
Packit |
90a5c9 |
s.async = true;
|
|
Packit |
90a5c9 |
s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
|
|
Packit |
90a5c9 |
(d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
d.write('
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
})(window, document);
|
|
Packit |
90a5c9 |
//--></script>
|
|
Packit |
90a5c9 |
Copyright 2018 The Apache Software Foundation. Licensed under the Apache License, Version 2.0.
|
|
Packit |
90a5c9 |
Modules | Directives | FAQ | Glossary | Sitemap <script type="text/javascript">
|
|
Packit |
90a5c9 |
if (typeof(prettyPrint) !== 'undefined') {
|
|
Packit |
90a5c9 |
prettyPrint();
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
//--></script>
|
|
Packit |
90a5c9 |
</body></html>
|