We can use .htaccess to redirect http://example.com to http://www.example.com by using the rewrite engine. But i find another simple way to implement the redirection just by setting up a VirtualHost.
Assume you have the current VirtualHost setting as follow.
<VirtualHost *:80> ServerName www.example.com DocumentRoot /var/www/<webroot>/ <Directory /var/www/<webroot>/> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>
You could setup a new VirtualHost as follow to redirect example.com to http://www.example.com.
<VirtualHost *:80> ServerAlias example.com RedirectMatch permanent ^/(.*) http://www.example.com/$1 </VirtualHost>
Done =)
Reference: StackOverflow – apache redirect from non www to www
Filed under: Apache Tagged: .htaccess, Apache, mod_rewrite, Postaday2011, VirtualHost
