Book Sale! Save 20% on WordPress books with discount code: SAVE20
Web Dev + WordPress + Security

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 = Fullstack Developer. Book Author. Teacher. Human Being.
USP Pro: Unlimited front-end forms for user-submitted posts and more.

2 responses to “Difference between mod_alias and mod_rewrite”

  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. Something to add? Let me know.
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
GA Pro: Add Google Analytics to WordPress like a pro.
On macOS when an app won’t hide, you can force it by holding the ⌥ (option) key and clicking anywhere on the desktop.
Currently having a blast redesigning Plugin Planet, so much work so little time.
Thanks to David McCan over at WebTNG for the awesome BBQ Pro review.
Enjoyed a nice mini-vacation with my fam. Great way to recharge and regroup.
Nice little interview with yours truly over at ThemeIsle. WordPress, web dev & more!
Perishable Press celebrating 16 years online! An incredible, rewarding journey.
Thanks to Nicholas Ferrell for his excellent and thorough review of Wutsearch search-engine launchpad.
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.