Why Redirect NON-WWW URLs to WWW?

Most of the users use either www or non-www version of their domain name to get their site in the browser. But some web servers or search engines may consider these URLs to be different. So there may be a possibility of content duplication. To avoid this duplication, redirect non-www URLs to www by using /.htaccess file from Apache server.

Usage of /.htaccess file:

/.htaccess file is used to override the main server configuration. The main usage of /.htaccess file is rewriting URLs, blocking, SSL, listing directory, controlling the cache and etc.

How to redirect non-www URLs to www using .htaccess file:

The first step is to create a file with the .htaccess extension in the root directory. This is a simple text file with any text editor.

Add the following code to the root .htaccess file.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Example: yourdomain.com will be redirected to www.yourdomain.com by adding this code as

RewriteCond %{HTTP_HOST} !^yourdomain.com
RewriteRule ^(.*)$ http://www.//yourdomain.com/$1 [R=301,L]

After this, when anyone access yourdomain.com will be redirected to www.yourdomain.com

Categorized in:

Tagged in:

, ,