Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
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.

About the Author
Jeff Starr = Web Developer. Book Author. Secretly Important.
SAC Pro: Unlimited chats.

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

    • 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 »
Digging Into WordPress: Take your WordPress skills to the next level.
Thoughts
I disabled AI in Google search results. It was making me lazy.
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources. Also: “The Web” is not a valid answer.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.