Welcome to the new design! Please report any bugs or issues, thanks :)
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.
Digging Into WordPress: Take your WordPress skills to the next level.

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
What's up with Plesk UI lately? Especially on Chrome it looks just awful, all kinds of broken. Come on Plesk devs get it together.
Things get stressful, I try to pray. Not always easy, but always helps to relax and regain focus.
Nice new speed checker at fastorslow.com.
Easy way to exclude certain tests from WP Site Health: Site Health Tool Manager
Excellent (and free) tool for getting tons of site SSL infos: whynopadlock.com
Everyone just stay home and hide forever. Brilliant idea.
Playing with Microsoft Edge browser on macOS. It's like 1998 all over again.