Apache: 301 redirect non-WWW to WWW URLs
First check if mod_rewrite module is enabled. If not, check this guide how to do it.
Edit .htaccess file from the document root of your website.
Add this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
If you need the opposite - redirect WWW URL to a non-WWW add this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Save changes.
Navigate to your web site in a browser without WWW. If you did it all correct WWW will be added in the address bar automatically.
See also:
Got a comment?