How to Redirect Preferred domain name (www or non-www)

How to Redirect Preferred domain name


How to redirect website domain name as www to non-www or non-www to www. If it’s hurt Google SEO.
Please provide a simple way to do this.
Non-www domains are not hurt your website SEO. Because the world’s most famous news blog Mashable is running under the non-www domain. But Google takes www and non-www domain as a different domain. But it’s not important for SEO. If you feel www is not suitable or compatible with your website or blog change non-www to www.
If you want to change your Preferred domain open your website hosting folder.
open .htaccess file.
add the following line to change domain: www to non-www

<IfModule mod_rewrite.c>
    RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

add the following line to change domain: non – www to www

<IfModule mod_rewrite.c>
    RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

Note: remove example and .com instead of your domain name.
Please refer to Google guide for Preferred domain.
Changing www or non-www domain name can affect Social sharing ( Google Plus, Facebook likes, Twitter tweets, etc)?
Yes, it affects Google plus sharing and Twitter tweets. But it doesn’t affect Facebook like or recommendations.
If you are using nginx, then use this code to redirect
non-www to www

server {
	listen	   80;
	if ($http_host = {
		rewrite  (.*)$1;

Read this document to find more details about nginx rewrite rules.
Will the redirect work if let’s say I did not add the CNAME record to DNS? I have already chosen my preferred domain in google per your guild above.
You don’t need to add any CNAME records.