The first parameter of RedirectPermanent
should be a path, not a full URL (e.g. RedirectPermanent /foo http://example.com/bar
), so you won't quite be able to get what you're trying to do to work.
You should be able to do it if you split the www part into a separate vhost:
<VirtualHost *:80>
DocumentRoot /var/www/example
ServerName example.com
<Directory "/var/www/example">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/example
ServerName www.example.com
RedirectPermanent / http://example.com
</VirtualHost>
However, this will only redirect the homepage. If you want to redirect all www.example.com
requests you'll want to use mod_rewrite as in closetnoc's example. You don't have to use htaccess files - those directives are perfectly valid in the directory block of a vhost:
ServerName example.com
ServerAlias www.example.com
<Directory "/var/www/example">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
</Directory>
Also, if you are concerned about the performance of .htaccess files (which is negligible, but it exists), you probably don't want to use AllowOverride All
, since that's what tells Apache to look for them.