Latest TweetsDisplay Your WordPress Site Statistics: Complete Guide…
Perishable Press

Difference between mod_alias and mod_rewrite

Most of the redirect techniques provided in my stupid .htaccess tricks article all use Apache’s alias module, mod_alias. You can also use mod_rewrite to redirect URLs. The main difference is that, with mod_alias, the server is responding to the client request with a redirect, so the client immediately is sent to the new location. Conversely, with mod_rewrite, the server simply returns the new content, so the client is not actually redirected anywhere. This makes mod_rewrite more advantageous because it happens transparently, requiring less work from the client (user). Let’s look at some more examples of rewriting/redirecting with mod_rewrite instead of mod_alias.

Redirecting with mod_rewrite

Here are some examples showing how to redirect (rewrite) with mod_rewrite:

# redirect old file to new on same domain
RewriteRule /old-file.html /new-file.html [L]

# redirect old directory to new on same domain
RewriteRule /old-dir/ /new-dir/ [L]

When using mod_rewrite, the key to rewriting instead of redirecting is not including the [R] flag in the RewriteRule. When [R] is included, Apache will redirect the client to the new location. When [R] is excluded, Apache will rewrite the request without redirecting the client. Thus, one consequence of excluding the [R] flag is that the user’s browser will show the originally requested URL in the address bar.

Redirecting external domains

Of course, mod_rewrite can also be used to redirect requests to external domains. Here are some examples:

# redirect old file to new on different domain
RewriteRule /old-file.html [R=301]

# redirect old directory to new on different domain
RewriteRule /old-dir/ [R=301]

# redirect old domain to new domain
RewriteRule / [R=301]

Notice the inclusion of the [R=301] flag. This tells Apache to redirect the request (instead of rewrite), and send a 301 “Permanent” status code. If no status code is specified, the server will use the default, 302 “Temporary”. More flags are possible, refer to the Regex Character Definitions for details.


Another benefit of using mod_rewrite instead of mod_alias, is that mod_rewrite gives you much more control over every aspect of the redirect. For example, you can use RewriteCond to exclude certain URLs from the redirect, or maybe redirect based on specific aspects of the request, such as the query string, user agents, or IP address. Surf around the site for more examples and tutorials about how to redirect with mod_rewrite and redirecting stuff with Apache/.htaccess in general.

Jeff Starr
About the Author Jeff Starr = Creative thinker. Passionate about free and open Web.
2 responses
  1. What is the advantage of using mod_rewrite above Redirect 301 when I want to redirect? Isn’t that where Redirect is for?

    RewriteRule /old-file.html /new-file.html [L]
    Redirect 301/old-file.html

    • Jeff Starr

      Flexibility and control are the main reasons. In general if you can do it with mod_alias is slightly better for performance (depending on the rule(s) in question. Also just for anyone else reading, the syntax is incorrect and not optimal in your redirect example. Should look more like this:

      Redirect 301 /old-file.html /new-file.html

      I.e., needs a space after the response code, and no need to specify the domain unless it is

[ Comments are closed for this post ]