Forum Moderators: phranque

Message Too Old, No Replies

301 Redirect Endless loop - Help!

         

seoplease

12:01 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



Hi,

I parked two domain names on the same hosting account. So both domains, when visited, load the same homepage (i.e. duplicate content). I then made a 301 redirect in the htaccess as follows:

Redirect 301 / http://www.example.com/

When i go to my old domain, say www.example.net, it 301s to www.example.com as the htaccess tells it to. But www.example.com also 301 redirects to www.example.com (i.e. ENDLESS LOOP!). So it is an endless loop because both domains share the same htaccess.

Im guessing i cant do it this way. Is there any other way to do a 301 from the old domain to new one, without buying another hosting account? Do i have to have two individual .htaccess files on different accounts in order to make it work? Whereby the domains being redirected are on one .htaccess file and the other main domain has its own htaccess file?

Cheers,
Luke

[edited by: jdMorgan at 12:46 pm (utc) on April 4, 2007]
[edit reason] Example.com [/edit]

jdMorgan

12:59 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Redirect 301 / http://www.example.com/

You get an endless loop because there is nothing in that code to tell it not to redirect www.example.com to itself.

You are correct that you cannot use the Redirect directive, and this is the reason; It is unconditional, and will cause a loop in the scenario you describe.

In order to prevent the loop, you must find a way to tell the code not to redirect www.example.com to itself. This can be done by using mod_rewrite, and specifically, the RewriteCond directive in mod_rewrite, to test the requested hostname and act accordingly:


Options +FollowSymLinks -MultiViews
RewriteEngine on
#
# if requested hostname is non-blank
RewriteCond %{HTTP_HOST} .
# and if requested hostname is NOT "www.example.com"
RewriteCond %{HTTP_HOST} !^www\.example\.com
# redirect to same object in correct domain
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

The first directive, Options, may or may not be required on your server. If it is not required, it may in fact not be allowed. Comment it out of you have trouble.

The second directive, RewriteEngine, is required once (and only once) at the top of your mod_rewrite code.

The third directive, the first RewriteCond, is only required if you do not use a name-based virtual (shared) server. It prevents an infinite loop if the client does not send a "Host" header with its request. Since it is impossible to access a name-based virtual server without a "Host" header, this line is not required on a name-based virtual server. No harm will come from leaving it in, except that it takes a little time to process it.

Note that this code will also redirect "example.com" to "www.example.com", and so serves to canonicalize your main domain name as well, preventing ranking dilution from having duplicate content on two variations of the domain.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

seoplease

11:10 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



thanks for that great response.

does this redirect www.abc.com to www.xyz.com though? i can only see one domain name in the code you wrote, so what stops it from looping?

also, i have about 50 domain names to redirect so if i park them all on the same server sharing the same htaccess file as the original domain, will the code above work?

thanks a lot

jdMorgan

11:33 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To understand the code, please read the comments in the code and the references cited. As written, it redirects anything except www.example.com to www.example.com. And once it does that, the domain is www.example.com, so it won't redirect again.

Jim