New Bookstore! Save 20% on books with discount code: LAUNCH
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 http://new-domain.com/new-file.html [R=301]

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

# redirect old domain to new domain
RewriteRule / http://new-domain.com/ [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.

Benefits

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 = Web Developer. Book Author. Secretly Important.
WP Themes In Depth: Deep dive into WP theme development.

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]
    against
    Redirect 301/old-file.html http://www.domain.com/new-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 new-domain.com.

Comments are closed for this post. Something to add? Let me know.
Welcome
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 »
BBQ Pro: The fastest firewall to protect your WordPress.
Thoughts
Take a screenshot with Firefox (no extension required). Open Developer Tools Settings and enable the “Take a screenshot” button. Then click the button :)
Take a screenshot with Chrome (no extension required). Open DevTools, type Cmd + Shift + P, then type screenshot.
After 10 years working on my 2010 iMac, my upgrade finally arrived. Shiny new iMac shipped from Ireland :)
Too much caffeine weirds me out. But I love the taste of coffee. So once in a while I enjoy a small cup of decaf. Hits the spot.
Chris Coyier is a truly awesome person. One of the finest people I've ever worked with. Just #gottasayit
Excel won't open CSV file because SYLK format? Open it with text editor and add an apostrophe ' at the beginning of the file, save changes, done.
Displaying too many social media buttons and links all over the place imho makes you look desperate and frankly kinda sad.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.