Redirect to Avoid Duplicate Sites

Sitepoint Members,
Is
RewriteCond %{HTTP_HOST} ^xyz.com
RewriteRule ^(.*)$ http://www.xyz.com/$1 [r=301,L]

still the best way to handle the www, as opposed to a 301 redirect?

Do you think I should redirect to www or redirect to no www. I think I’m going to redirect to No www, it’s a cleaner look, “www” is not so new anymore.

If so, then I would have to use:
RewriteCond %{HTTP_HOST} ^www.xyz.com
RewriteRule ^(.*)$ http://xyz.com/$1 [r=301,L]

right?

Thanks,

Chris

@Chris77 - I’ve moved this to the “Server Configuration, Apache & URL Rewriting” forum where hopefully you’ll get better answers.

Chris,

Regards,

DK

dklynn,
For www entered into a browser and changed to no www,

code like this?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.xyz\.com [NC]
RewriteRule .? http://xyz.com%{REQUEST_URI} [R=301,L]

Thanks,

Chris

dklynn,
I put in
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.xyz.com
RewriteRule ^(.*)$ http://xyz.com/$1 [r=301,L]

and entering a www address into a browser stayed at a www address.

Chris

dklynn,
I tried that again like this

RewriteCond %{HTTP_HOST} ^www.xyz\.com [NC]
RewriteRule .? http://xyz.com%{REQUEST_URI} [R=301,L]

and it had no effect.

Chris

Chris,

First, escape the dot character in your {HTTP_HOST} regex.

Second, that would have no effect on the mod_rewrite code so it’s likely that:

  1. mod_rewrite is not enabled. If this were the case, though, you’d have received a 505 error message (because the mod_rewrite code would not have been understood.

  2. Your host has not enabled the use of .htaccess on your server.

  3. You are being hosted on an M$ server (i.e., not Apache).

To be sure, read the part in my tutorial about setting up a server and TESTING that mod_rewrite is enabled and functioning. Run the simple test then come back with the results.

Regards,

DK

dklynn,
What I eneded up getting to work was

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.xyz\.com [NC]
RewriteRule ^(.*)$ http://xyz.com/$1 [L,R=301]

Chris

Chris,

Same thing except slower because my .? version sending the %{REQUEST_URI} is the SAME as capturing the {REQUEST_URI} to $1 and adding that to the domain redirection. Six of one, half dozen of the other as it’s only slightly more than a matter of style.

Regards,

DK

If it’s faster I’d like to use it but you gave me no www to www and I need www to no www. I tried to write it with your help but could get it to work.

How about this:

RewriteCond %{HTTP_HOST} ^www\.xyz\.com [NC]
RewriteRule .? http://xyz.com%{REQUEST_URI} [R=301,L]

Thanks,

Chris

Chris,

Ahhhhh, perfection!

Oh, I’ve assumed that you’ll retain the previous RewriteEngine on statement.

Regards,

DK

DK,

I put

RewriteCond %{HTTP_HOST} ^www\.xyz\.com [NC]
RewriteRule .? http://xyz\.com%{REQUEST_URI} [R=301,L]

in the htaccess file with that slash (red) because earlier you said something about closing the dot.

Should that slash be there? The code is working with the slash.

Thanks,

Chris

Chris,

Whoops! There should be NO backslashes in the redirection, ONLY IN {regular expressions}. In that code, the RewriteCond’s ^www\.xyz\.com$ is regex and .? in the RewriteRule is regex.

Regards,

DK

So
RewriteCond %{HTTP_HOST} ^www\.xyz\.com [NC]
RewriteRule .? http://xyz\.com%{REQUEST_URI} [R=301,L]

Shold be?
RewriteCond %{HTTP_HOST} ^www\.xyz\.com$ [NC]
RewriteRule .? http://xyz.com%{REQUEST_URI} [R=301,L] (no \)

Thanks,

Chris

Chris,

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\\.xyz\\.com$ [NC]
# match {HTTP_HOST} start-of-string www dot xyz dot com end-of-string No Case
RewriteRule .? http://xyz.com%{REQUEST_URI} [R=301,L]
# match anything and permanently redirect to http://xyz.com with the same request ({REQUEST_URI} string)

Yup, it’s as easy as that!

Regards,

DK

DK,
That works fine, although it didn’t look like it worked at first because IE holds on to its cached copy much more that FF. You have to use Ctrl F5 regularly with IE when testing pages.

Thanks for the help,

Chris

IE is known as a PITA - but mostly for the lack of DOM compliance. Yes, cache on IE is another major problem (when testing).

Regards,

DK