Forien
Forien

Reputation: 2763

RewriteRule versus Redirect - which is better?

Intro

For a very very long time I have used RewriteRule as redirection command. For example I used this:

RewriteRule ^some-page.html$ /new-page.html [R=301,L]

But recently I've found other method, using Redirect keyword:

Redirect 301 /some-page.html /new-page.html

 

Question

Which method is better and should be used as primary tool?

Better is considered by being:

Upvotes: 3

Views: 1547

Answers (2)

MrWhite
MrWhite

Reputation: 45829

I agree with @anubhava, RewriteRule is far more powerful and more useful as a general tool. However, to answer your specific "better" criteria:

more stable

Nothing in it. Redirect (mod_alias) has been around longer. (?)

safer

Again, if you know what you're doing then one isn't any "safer" than the other. RewriteRule is more complex so there is more room for error. One thing you should be wary of, however, is that you should never use Redirect (mod_alias) and RewriteRule (mod_rewrite) together in the same file. They belong to different modules and execute at different times. Using them together can produce unexpected results.

faster

Some would argue that Redirect is faster. It is a lot simpler.

more acknowledged by 'community'

Debatable. RewriteRule would seem to be used more these days, but it does a lot more. And many will use it because that is what they always use. The Apache docs do recommend that if you are just doing a simple redirect then a mod_alias Redirect is preferred:

When not to use mod_rewrite

You should note that the two directives you posted above are not exactly the same. Redirect is prefix matching and anything after the match is automatically passed to the target URL. Whereas in your RewriteRule you have used anchors to explicitly mark the start and end of the URL.

Upvotes: 4

anubhava
anubhava

Reputation: 785186

Beyond any doubt RewriteRule will serve you better as it has regex support and has many more goodies available in mod_rewrite module. Other one is older mod_alias module and has no support for many important things like:

  1. No RewriteCond for example matching query string
  2. No internal rewrite
  3. No support of env variables
  4. No way to use proxy
  5. No support of flags such as NE, NC, QSA etc.

List will be pretty long but all said and done you can rely on mod_rewrite to serve you better.

Upvotes: 3

Related Questions