Server Fault is a question and answer site for system and network administrators. 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'm running CentOS 6.0 and am trying to make mod_rewrite remove the www from any URL's prefixed with it. Here's the code I have in my httpd.conf:

NOTE: I am using a VPS with full root access so I am not using .htaccess files or any "per-directory" settings.

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

This is working fine for all pages except that the URL will be written to (notice the two slashes) or Can anyone help me figure out why this is happening?

share|improve this question
up vote 2 down vote accepted

Since it's in your main config file, and not a per-directory (<Directory> or .htaccess), then the leading slash is not being stripped for context. It's actually adding (or trying to add - not sure if Apache's removing the extra in most cases, or if the client browser is) the second slash to every rewrite.

Just account for the fact that the leading slash is included in the string that you're matching:

RewriteRule ^/(.*)$$1 [R=301,L]
share|improve this answer
Thanks! That did the trick. I never ran into this issue when using Ubuntu Server or Fedora so I was rather stumped why it was happening on CentOS. – Aaron Nov 21 '11 at 22:35

Since it's directly in a vhost, here's the solution: remove the slash here:$1

Another tip: when alone, ^(.*)$ is the same as (.*).

So here's the solution:

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

Please tell me if it worked.


share|improve this answer
Thanks for your answer Olivier. Even though the above answer solved my problem first, I tried out your syntax to see if it would produce the same result and it worked fine :) – Aaron Nov 21 '11 at 22:39

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.