redirecting www to non www

Crunchy

Member
There seems to be some misleading info out there on the best way to redirect the domains.

What would be the best way to redirect http://www.domain.com/ to http://domain.com/

I saw the following in another host's tutorial on using the cpanel 301 redirect applet:

Warning: Never create a redirect for domain.com to www.domain.com or from domain.com to domain.com/index.something. Doing either will cause an infinite loop. If you need to control the www and index page, you must use mod_rewrite by adding .htaccess code.

Which would suggest something like this?

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

A bit confusing for the coding challenged....

Frankly, I see it as virtually irrelevant since the vast majority of searches are through search engines, but I guess I'm a bit of a perfectionist....
 
I believe that code would work. This would also work:

Code:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

As usual, there are multiple was to accomplish this within htaccess.

Let us know if you need more help :)
 
OK, so can I simply copy and paste that into the htaccess file - meaning I don't have to put the actual domain into it?

Also, is there a specific spot I should put it within the htaccess file? Before or after anything? Probably on their own lines at least....

Just looking at it the first line says if the domain has www, then use following rule which is to redirect to non-www domain. Hmmm, maybe my nephew was right. They do all seem to use similar syntax.
 
Crunchy, use this code (inserting your domain in the appropriate locations, of course) -

Code:
RewriteCond %{HTTP_HOST} !^YOURDOMAIN\.com$
RewriteRule ^(.*)$ http://YOURDOMAIN.com/$1 [L,R=301]
 
Thanks, Mike. It ended up being something else altogether actually....

The nameservers evidently got messed up when their old registrar was bought out by another. I have tried to get them to transfer to namecheap, but no success yet.

HEY! I didn't even notice the Purdue logo on the penguin. Big win last Saturday, eh?
 
Top