Resolving a website from both www and non-www is seen by search engines as the two different URLs serving the same content. And it actually very dangerous for the websites in regards to the search engine optimization. As a result of which search engine may penalise the website for serving duplicate content. However the solution is very simple
WHY IT IS IMPORTANT?
Having two different versions of URL for the same page i.e. yourdomain.com and www.yourdomain.com is considered as the duplicating the same content on two different web pages. Duplicate content ( after the Panda Update of Google) lead to penalty and your website may disappear from SERP. WHY? This is what the entire search engine algorithm are designed with. All the search engine bots are programmed to consider all the unique URLs as a different entity. They do not have functionality to correlate 2 different versions of the same or different URLs until you (the webmaster of website) mention that.
HOW TO DO IT?
STEP 1: Login to your website control Panel, i.e. cPanel
STEP 2: Go to the FILES category and click on File Manager.
STEP 3: A popup will open asking you what Directory you want to go to. Check the Web Root radio button and also check the check box displaying Show Hidden Files (dotfiles).
STEP 4: Here you can see a file named .htaccess. Select this file and click on Edit button placed at top right navigation. A popup will open asking about the encoding, simply click on Edit again.
STEP 5: Now a text editor will open the .htaccess file, place the below code as per your requirement in the file. Click on Save Changes[/vc_column_text]
TO REDIRECT ON WWW.YOURDOMAIN.COM
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301,NC]
TO REDIRECT ON YOURDOMAIN.COM
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com [NC]
RewriteRule ^(.*)$ https://yourdomain.com/$1 [L,R=301]