
WWW to Non WWW and Non WWW to WWW redirect with HTACCESS
In this tutorial we'll create a snippet that will redirect all hits to WWW version of your website to Non WWW version and reverse.

It's quiet common that Google webmaster tools can tell that few page of your website have duplicate contents or even your whole website and its even provides link to same page, one with a WWW and other without it. The reason for Google bolt to take it as duplicate content although it links to the same page because all the bolts the WWW version and non WWW version as two different sites.
Having duplicate contents can lead to complete removal of your website from the index of search engine, so it's important to stick to one version, but that is difficult to achieve that because it's quite common people link to both www and on-www versions of a domain and the search engine can come across it and find that it's a duplicate of page with non WWW. This is known as the www/non-www canonical issue.
It really doesn't matter if you use www.yoursite.com or yoursite.com. In Tech Stram we use the non WWW version however many people prefer to drop it, it's really up to you.
You can overcome this by Force users to use non WWW version or WWW version the one which you prefer. If you are planning to start you can choose any may be a coin toss can be of used to determine it. But it you website is going well and is already crawled by search engines, we recommend you to do some checking In different search engine and find out which version is widely used and make it permanent, then force users to use that.
Solve www/non-www canonical issue
You can achieve that with a 301 permanent redirect. We have provided the code for Apache with HTACCESS and httpd.con and also ASP below.
Make sure that you backup the .HTACCESS file before you proceed. Incorrect codes can lead to 500 errors.
WWW to NON WWW Redirect for Apache
You can add the following code to your .htaccess file, you can find it in your website root directory, if you don't find it you can copy this in a text editor and save it as .htaccess, then upload it.
Redirect non-www urls to www with HTACCESS
The following code makes a 301 redirect to WWW version of your website, it redirects users and also bolts informing them bolts that content in those pages are moved Permanently.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.yoursite\.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
You can also use RewriteCond %{HTTP_HOST} ^example\.com [NC] instead of RewriteCond %{HTTP_HOST} !^www\.yoursite\.com. Both serve the same.
non-www urls to www with https using HTACCESS
To force non-www urls www with https
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Redirect www urls to non-www for APACHE with HTACCESS
The following code make a permenant 301 redirect to non-www version.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
You can also use RewriteCond %{HTTP_HOST} ^example\.com [NC] instead of RewriteCond %{HTTP_HOST} !^www\.yoursite\.com. Both serve the same.
Redirect www urls to non-www with https using HTACCESS
If you need to force https along with non-WWW you can use the code below
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
WWW to NON WWW Redirect for Apache with httpd.con
IF you don't have access to .HTACCESS you can use httpd.con, most free web host don't provide access to HTACCESS
<VirtualHost *>
ServerName www.example.com
Redirect 301 / http://example.com/
</VirtualHost>
NON WWW to WWW Redirect for Apache with httpd.con
<VirtualHost *>
ServerName example.com
Redirect 301 / http://www.example.com/
</VirtualHost>
Many popular scripts, particular content management systems (CMS's) edit the .htaccess file and add their own redirection so you may not have to add any of the code noted above. Adding these again can lead to 500 errors or improper redirection
Microsoft IIS Web Server
You need to add the code given below to a file that is included on all the pages, or directely on top of each file
If InStr(LCase(Request.ServerVariables("SERVER_NAME")),"www") = 0 Then
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "http://www.domain.com"
End If
Note: these instructions require administrative access to IIS. If you do not have this access (e.g., if you have a shared hosting account on a Windows server), you should use one of the server-side scripting methods such as ASP or PHP given further below.
The above code for IIS server has not been used by our systems so we do not guarantee that it works in all situations.