Redirect Domain to Include or Exclude www

This guide is based on the .htaccess file which is a directory-level configuration file supported by several web servers such as Apache web server. It is used for configuration of website resources such as URL redirection, URL shortening, access control, and more. The ‘dot’ before the file name makes it a hidden file in Unix-based environments. You can read more about this file at Wikipedia.

Using the .htaccess file in the main domain directory, you can either redirect all requests to sanuja.com or www.sanuja.com. This type of redirection is known as a HTTP 301 redirect.


www URLs to non-www

Add the following lines to your .htaccess file to change from www.sanuja.com to sanuja.com.

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

Remember to replace “sanuja.com” part with your domain name URL.

The above code will result in directing all request to https://www.sanuja.com/[what ever the page link] to https://sanuja.com/[what ever the page link].

non-www URLs to www

Add the following lines to your .htaccess file to change from sanuja.com to www.sanuja.com.

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

Remember to replace “sanuja.com” part with your domain name URL.

The above code will result in directing all request to https://sanuja.com/[what ever the page link]. to https://www.sanuja.com/[what ever the page link].

Conclusion

Organizations such as CBC and BBC redirect all requests with www dot prefix. Most likely this procedure is used because historically all websites on the World Wide Web (WWW) used the www dot prefix. Even newer entities such as Facebook uses www dot prefix. Twitter for example drops the Ws. There are slight advantages to using www prefix. I am not going into details and you can do some research yourself. Personally, I have set up sanuja.com to load without the Ws.