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]

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.

  • 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. 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. Mar 22 '11 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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