This article explains how to redirect your website from the non www url to the www url but editing the .htaccess file. This is necessary because search engines think that example.com and www.example.com are different websites. But since they both have the same content it might work negatively on the search engine ranking. So it is better to redirect example.com to www.example.com doing so this sends HTTP 301 status code when a search engine (or any user) accesses example.com. The 301 code says the non www domain has been “Moved Permanently” to the www domain. Before proceeding make sure the server has support for mod_rewrite.
Redirect non www to www htaccess
If it doesn’t already exist create a file named .htaccess (notice the dot at the beginning) inside the Document Root i.e., the www or public_html or htdocs folders. If you already have the .htaccess file edit it using a text editor. Add the following line to the .htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Replace example.com with your domain name. Save the .htaccess file. Test the configuration by typing example.com in your browser and check whether it redirects to www.example.com. Now to technically test it. Go to HTTP Headers Lookup Tool enter your domain name WITHOUT www and you should see something like this
Take a look at the first two lines. The first displays the status code and second shows where it redirects to. Note to Joomla users – if you are using Joomla’s SEO friendly URLs feature using .htaccess file there is no need to enter the first line (RewriteEngine On) again as the .htaccess file shipped with Joomla already contains this.
Leave a Reply