Forum Moderators: phranque

Message Too Old, No Replies

www redirect conflict with other rules

         

micmacmic

3:28 pm on Jan 6, 2010 (gmt 0)

10+ Year Member



Hi,

I have a (new) .htaccess redirect problem...

I have moved some domains from an old CMS to WPMU and I have setup redirects frrm old URL's just going to the new front page.

To accomplish this I have setup redirects like this in my .htaccess:

RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com [NC]
RewriteCond %{QUERY_STRING} ^n=(.*)$ [NC]
RewriteRule ^link_details\.php$ [domain1.com...] [R=301,L]

RewriteCond %{HTTP_HOST} ^(www\.)?domain2.com [NC]
RewriteCond %{QUERY_STRING} ^n=(.*)$ [NC]
RewriteRule ^link_details\.php$ [domain2.com...] [R=301,L]

This seems to be working fine - when I click the old indexed pages in Google I get to my new front pages.

But now I just discovered that all my pages exist with both www and none www version.

So I wanted to setup a general rule that will keep only either the none www version or the www version (I don't care which).

So I found this code:

RewriteCond %{HTTP_HOST} !^(www\.domain1\.com)?$
RewriteRule ^(.*)$ [domain1.com...] [R=301,L]

But if I put this in my .htaccess I get conflicts between the domains - for example www.domain2.com in the browser will bring up www.domain1.com.

So should the www ReWriteRule be integrated in the QUERY_STRING rule or how will I accomplish to put things in the right order so that I don't get conflicts between the different domains?

Thanks!

jdMorgan

5:52 pm on Jan 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem appears to be simple: Your 'link_details' rules both specify "domain1.com" as the target (and presumably canonical) domain, while your new domain canonicalization rule specifies "www.domain1.com" as the canonical domain.

Your new rule says, "If the requested hostname is NOT exactly "www.domain1.com" or blank, then redirect the request to "www.domain1.com."

Remove the "www." from both lines of the new rule, put the new rule after your two existing "link_details" rules, completely flush (delete) your browser cache, and test again.

This is probably a good time to remind everyone that cutting and pasting code is not safe. Don't paste code into your server configuration files unless you understand the meaning and effect of every single character in that code. If necessary, analyze the code character-by-character using the resources cited in our Apache Forum Charter, and once the function is understood, step back and ask the question, "How will this function affect any and all aspects of serving requests on my site?"

Otherwise, you're playing with a loaded shotgun in the dark: One single typo, tiny logic or rule-order error, or inappropriate function can ruin your search rankings and/or confuse your visitors -- and possibly put you out of business.

In general, put all of your external redirect rules first (those ending with [R=30x,L] flags or containing a full URL in the RewriteRule substitution field), ordered from most-specific patterns and conditions (fewest URL requests affected) to least-specific patterns and conditions (more or all URL requests affected). Follow these external redirect rules with all of your internal rewrite rules, again in order from most- to least-specific patterns and conditions.

Jim

micmacmic

11:17 pm on Jan 6, 2010 (gmt 0)

10+ Year Member



OK,

So in my excisting link_details rule I redirect to the none www version:

RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com [NC]
RewriteCond %{QUERY_STRING} ^n=(.*)$ [NC]
RewriteRule ^link_details\.php$ [domain1.com...] [R=301]

I can understand what each line does here. I'm really having a hard time understanding how the second none www rule works. The destination should go to the none www version:

RewriteRule ^(.*)$ [domain1.com...] [R=301,L]

And this one should be put after the link_details rule.

But I don't understand if I need a second RewriteCond %{HTTP_HOST} line and what it should look like. So I definitely need some help on that part before I create more redirect-loops...

jdMorgan

7:48 am on Jan 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like you really only need two rules here:

RewriteCond %{QUERY_STRING} ^n= [NC]
RewriteCond %{HTTP_HOST} (domain1\.com¦domain2\.com) [NC]
RewriteRule ^link_details\.php$ http://%1/? [R=301,L]
#
RewriteCond %{HTTP_HOST} ^www\.(domain1\.com¦domain2\.com) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Replace the broken pipe "¦" characters above with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

micmacmic

1:45 pm on Jan 7, 2010 (gmt 0)

10+ Year Member




1. It works!
2. It is a much more elegant set of expressions - I'm have now compressed several lines for each domain in my .htaccess to just these five lines.
3. You have saved me an unknown number of hours and most likely also my users from a few more redirect loops...

Thanks Jim , this was a very big help for me!