Redirect www to server name

I’ve been having some trouble trying to get mod_rewrite to redirect www.domain.com to domain.com. I’m not sure if the subdomain logic in the virtual host has anything to do with that.

One method I had tried:

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

This works but it puts a // at the end of the domain. Even if I go to something like http://www.domain.com/folder/page.htm it would redirect to http://domain.com//folder/page.htm. This also makes me think about HTTPS requests, if it would even work with those or not. I haven’t tested this on a SSL enabled site yet.

I’d like to have a rule that redirects www.domain.com/anything to domain.com/anything and supports either HTTP or HTTPS requests.

How would I go about creating this to work with InterWorx sites?

Thanks.

Well I fixed the // issue by adding a / before the (.*):


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

I can’t seem to get this to work with SSL enabled sites though.

Any thoughts?

Okay well I got this to work with the SSL site that I was testing. I just copied the mod_rewrite rule into the port 443 virtual host for the site and changed http to https… This wasn’t loading the SSL pages for me at first because the config file for the application running the site still had www set for the SSL connection… it had nothing to do with the mod_rewrite rule.

For port 80 virtual hosts:

# Redirect www.domain.com to domain.com
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^/(.*)$ http://domain.com/$1 [R=301,L]

For port 443 virtual hosts:

# Redirect www.domain.com to domain.com
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^/(.*)$ https://domain.com/$1 [R=301,L]

And if you want to redirect it the other way, domain.com to www.domain.com it should be self-explanatory. I’m just redirecting it all to domain.com because that IS that ServerName in the virtual host file. I also request certificates under the common name domain.com rather than www.domain.com. Just a personal preference.

My question still is if this is going to interfere with any of the InterWorx subdomain logic in the virtual host files?

:slight_smile:

Not sure about interfere’n with Iworx but I agree with you on killing the www :slight_smile:

Thank god, they didn’t design it as “xxx” or “wwwwwwwwwwww”. :slight_smile: