3

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?

2 Answers 2

3

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]
1
  • 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, 2011 at 22:35
5

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

1
  • 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, 2011 at 22:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .