Forum Moderators: phranque

Message Too Old, No Replies

types of redirect 301

         

mrgubu

8:28 am on Jun 1, 2010 (gmt 0)

10+ Year Member



Hello, I want to redirect one domain to another in the way
[olddomain.com...] -> [newdomain.com...]

The issue is that i have seen at least 3 different ways to do this and I would like to know what is the best way to do this for google SEO.

1. RewriteRule (.*) [newdomain.com...] [R=301,QSA,L]

2. redirectMatch 301 ^(.*)$ [newdomain.com$1...]

3. redirect 301 (.*)$ [newdomain.com...]

Which one is the best/the right one?

Thanks.

jdMorgan

4:13 pm on Jun 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



They are all equivalent, although the [QSA] flag on the first RewriteRule is unnecessary.

If you use (or plan to use, or might use) mod_rewrite for other purposes, I strongly suggest that you do not mix RewriteRule with Redirect and RedirectMatch directives.

Because RewriteRule is processed by mod_rewrite, while Redirect and RedirectMatch are processed by mod_alias, mixing the mod_rewrite and mod_alias directives can result in unpredictable order of execution, especially if the server is upgraded or re-configured after your code has been tested and found to work.

This is because each Apache module takes turns scanning your .htaccess code for directives that it recognizes, and executes only the ones that it understands. Therefore the directives in your .htaccess file are executed on a per-module basis, and not strictly in the order that you wrote those directives in your file.

If mod_rewrite executes before mod_alias, then some serious problems can arise. If the reverse, there can still be a few problems, but perhaps not so serious once you are aware of the execution order -- However, since the execution order is set by the server config, it could possibly change and cause trouble in the future if your server is "upgraded" or re-configured by your host.

The most serious issue is when a RewriteRule is used for an internal rewrite (URL-to-filepath translation) and a Redirect or RedirectMatch is used for an external redirect (URL-to-URL substitution). The result can be that RewriteRule executes first, changing the current path (as is intended) to an internal filepath. If a Redirect or RedirectMatch executes after this, it will 'expose' that internal filepath as a URL to the client (e.g. search engine robot or browser), and the result can be to make a big mess of your search engine listings and rankings.

Therefore, I suggest that if you use mod_rewrite for *anything,* then use it for *everything.*

Jim

mrgubu

4:34 pm on Jun 1, 2010 (gmt 0)

10+ Year Member



Thank you for your detailed explanation Jim. I understand what you say.
However my plan was to use only one of the three alternatives that I exposed to redirect the whole site to other domain, and not to use the .htaccess for anything else.
So as you say they are all equivalent, I will use the first one (RewriteRule), only to take caution for the future, thinking that 'if you use mod_rewrite for *anything,* then use it for *everything.* '.

jdMorgan

4:41 pm on Jun 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since you're redirecting the entire domain, #3 would be the simplest *if* the old and new domains are on different servers:

Redirect 301 / http://example.com/

While I am a big fan of mod_rewrite and its power, it does take one or two additional directives to set it up and enable it, so the simple Redirect is a "better" one-line solution in that case.

Jim

mrgubu

4:51 pm on Jun 1, 2010 (gmt 0)

10+ Year Member



Thank you very much!