Enable the Apache2 rewrite Module on a debian system.
# cd /etc/apache2/mods-available/ # a2enmod rewrite # service apache2 restart
Example of an .htaccess that forwards www.example.com to example.com
RewriteEngine on RewriteCond %{HTTP_HOST} www.example.com RewriteRule ^(.*)$ http://example.com/$1 [R=permanent,L]
You may need to allow overwrite e.g.
<Directory "/sites/www.example.com/wwwroot"> AllowOverride All Order allow,deny allow from all </Directory>
Or skip the .htaccess in the wwwroot dir and just put the Rewrite rules in the apache host configuration file.
<Directory "/sites/www.example.com/wwwroot"> RewriteEngine on RewriteCond %{HTTP_HOST} www.example.com RewriteRule ^(.*)$ http://example.com/$1 [R=permanent,L] </Directory>
The .htaccess is read by apache every time someone visits the URL. I would use it only if I was creting an apache host for someone who does not have root or someone who does not have experience with systems administration.
apache mod_rewrite manual
mod rewrite example