Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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 ^/(.*)$ http://aaronjwood.com/$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: 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

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

 
discard

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.