Search code examples
.htaccesshttp-redirecthttp-status-code-301

.htaccess for 301 redirect: which syntax is best?


I am permanently redirecting my website

http://www.oldsite.com

to

http://newsite.com/blog

Is there a difference between using

Redirect 301 / http://newsite.com/blog/

or

RewriteEngine On 
RewriteRule ^(.*)$ http://newsite.com/blog/$1 [R=301,L]

Any reason I should use one over the other?


Solution

  • The first uses Apache's internal redirection engine to direct all requests to / to http://newsite.com/blog with a 301 Moved Permanently response code.

    The other loads the Apache rewriting engine and rewrites all of the incoming requests that match ^(.*)$ to http://newsite.com/blog/ (appending the matched part of the request URI to the target URI) with a 301 Moved Permanently response code, like the former.

    The difference? The former rewrites everything to http://newsite.com/blog/ regardless of the request, and the second takes into account the request URI rewriting it as specified. The first is also somewhat faster than the second because it does not load the rewriting engine, does not introspect the request itself, and (depending on the AllowOverride setting) does not have to look up and load .htaccess files.