<-
Apache > HTTP Server > Documentation > Rewrite

When not to use mod_rewrite 1d6ei

Available Languages:  fr 

This document supplements the mod_rewrite - namely, when to avoid using it.

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 mod_rewrite.

 Apache!

See also 27136x

top

Simple Redirection 42n3i

RewriteRule. 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. 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:

Redirect "/one/" "http://one.example.com/"

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:

<VirtualHost *:80>
    ServerName www.example.com
    Redirect "/" "https://www.example.com/"
</VirtualHost>

<VirtualHost *:443>
    ServerName www.example.com
    # ... SSL configuration goes here
</VirtualHost>

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.

top

URL Aliasing 4n5i21

The Alias is the preferred method, for reasons of simplicity and performance.

Using Alias 5b1z58

Alias "/cats" "/var/www/virtualhosts/felines/htdocs"

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.

top

Virtual Hosting 1f146x

Although it is possible to handle mod_vhost_alias to create these hosts automatically.

Modules such as mod_macro are also useful for creating a large number of virtual hosts dynamically.

Using mod_rewrite for vitualhost creation may be appropriate if you are using a hosting service that does not provide you access to the server configuration files, and you are therefore restricted to configuration 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.

top

Simple Proxying 1b5523

mod_proxy.

RewriteRule "^/?images(.*)" "http://imageserver.local/images$1" [P]

However, in many cases, when there is no actual pattern matching needed, as in the example shown above, the Proxy directive is a better choice. The example here could be rendered as:

Proxy "/images/" "http://imageserver.local/images/"

Note that whether you use ProxyReverse directive to catch redirects issued from the back-end server:

ProxyReverse "/images/" "http://imageserver.local/images/"

You may need to use RewriteRule instead when there are other RewriteRules in effect in the same scope, as a RewriteRule will usually take effect before a Proxy, and so may preempt what you're trying to accomplish.

top

Environment Variable Testing 6r5q41

<If>.

Consider, for example, the common scenario where RewriteRule is used to enforce a canonical hostname, such as www.example.com instead of example.com. This can be done using the <If> directive, as shown here:

<If "req('Host') != 'www.example.com'">
    Redirect "/" "http://www.example.com/"
</If>

This technique can be used to take actions based on any request header, response header, or environment variable, replacing mod_rewrite in many common scenarios.

See especially the <If> sections, and in certain other directives.

Available Languages:  fr 

top

Comments 2p1l6j

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 by our s 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 Libera.chat, or sent to our mailing lists.