Redirect URL within Apache VirtualHost?

Posted by DisgruntledGoat on Server Fault See other posts from Server Fault or by DisgruntledGoat
Published on 2010-03-08T20:56:25Z Indexed on 2010/03/08 21:08 UTC
Read the original article Hit count: 452

I have a dedicated server with Apache, on which I've set up some VirtualHosts. I've set up one to handle the www domain as well as the non-www domain.

My VH .conf file for the www:

<VirtualHost *>
  DocumentRoot /var/www/site
  ServerName www.example.com
  <Directory "/var/www/site">
    allow from all
  </Directory>
</VirtualHost>

With this .htaccess:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

Is there a simple way to redirect the www to the non-www version? Currently I'm sending both versions to the same DocumentRoot and using .htaccess but I'm sure I must be able to do it in the VirtualHost file.

© Server Fault or respective owner

Related posts about apache

Related posts about virtualhosts