Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to get rid of the "www" in front of my domain, and I'm trying the following, but it's not working:

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

Basically, what I hope to accomplish is that someone going to www.example.com would be redirected to example.com.

Any ideas as to what I'm doing wrong?

I've even tried substituting the second line for this:

RewriteCond %{HTTP_POST} www\.example\.com [NC]
share|improve this question

migrated from serverfault.com Mar 22 '11 at 1:19

This question came from our site for system and network administrators.

up vote 6 down vote accepted

Assuming your .htaccess is on the right place with the right permissions and your Apache server is configured to use it and mod_rewrite is enabled:

RewriteEngine on

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

Note the HTTP_HOST vs HTTP_POST in your example and that I use a 301 redirect. This is better since some links may point to the www version and this will let them count towards your SEO ranking. Search engines will eventually always send people to the right place.

share|improve this answer
I actually have it in a sites conf file in /etc/apache2/sites-available. This didn't work. I can get it to work the other way around (adding the "www"), but not getting rid of the CN. – supercheetah Mar 22 '11 at 5:26
Nevermind, I figured out what the problem was. I needed to create a separate conf file for that CN and redirect from there. – supercheetah Mar 22 '11 at 9:41
So then choose this answer and your accept answer. :) – John Conde Mar 22 '11 at 14:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.