PDA

View Full Version : Removing www from URL



Hylo
09-28-2006, 03:40 AM
I want all of my URLs to redirect the www version to the non "www" version and found this htaccess code to do it:

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

I have forums on a forums.mydomain.com subdomain though and it's removing the sub domain when I use this code.

Is there any way around this with mod_rewrite or will I have to content myself with using mydomain.com/forums instead of the sub domain?

Chris
09-28-2006, 12:35 PM
I'm not a mod_rewrite expect but the problem is that you're looking for a wildcard and changing it.

Changing your forum URL would be one idea, and I usually recommend not using subdomains anyways unless the subdomain is a physically different server, however you'd lose a lot of incoming links (maybe... how established is it?).

I imagine you can tweak it to forward correctly, but again, I'm not exactly where where to make the change.

Also be sure to use Google Webmaster Central to tell Google which version you prefer.

James
09-28-2006, 01:09 PM
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mydomain.com$ [NC]
RewriteRule (.*) http://mydomain.com/$1 [R=301,L]

Isn't that all you need? You're just trying to take out the www., so *shrugs*

Johnny Gulag
09-28-2006, 02:14 PM
I use the following and have never had any trouble, though as Chris says I am no Apache expert.


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

James
09-28-2006, 03:54 PM
what are the backslashes infront of the dots for?

Johnny Gulag
09-28-2006, 04:30 PM
what are the backslashes infront of the dots for? No idea. :p

stymiee
09-28-2006, 08:27 PM
They escape the period so it literally means "period". Otherwise the period means "any character" in regular expressions.