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 ^(.*)$ http://aaronjwood.com/$1 [R=301,L]

This is working fine for all pages except that the URL will be written to http://aaronjwood.com// (notice the two slashes) or http://aaronjwood.com//contact.php. Can anyone help me figure out why this is happening?

up vote 3 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 ^/(.*)$ http://aaronjwood.com/$1 [R=301,L]
  • 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: http://aaronjwood.com$1

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

So here's the solution:

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

Please tell me if it worked.

Olivier

  • 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 clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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