John's answer is spot on however if your looking for advice which one is technically the best then the actual the answer is all 3 options.
SOURCE: Pro Webmasters Answer from John Conde
- Do a 301 redirect to use the 'www' or no 'www'
- Specify your preferred domain in Google Webmaster Tool
- Use canonical URLs (although it isn't typically used in this situation
And here's why...
Why 301 redirect URLs:
When you force users to use either www
or non-www
and they decide to reward your site with a backlink the next user who clicks that link does not need to go through a redirect.
While redirects nowadays are near instant they are additional server-side request that can be reduced if you have users linking to you correctly, which should be considered a good practice, especially if your site receives high volume of traffic.
Why canonical URLs
Nowadays it is considered a best practice to use canonical links, this is because duplicate content can appear in many different forms without factoring www and non-www. Blogs particularly will operate with pages being accessible via many different URLS for example:
Using Canonical links correctly will insure that your content however it may be accessed is never marked as duplicate.
Why preferred domain in Google Console
It is a good practice to add all variations to Google Webmaster Tools so that you can confirm at any point that your site or Google is not doing anything it shouldn't be.
- NON-SSL sites will have 2 variations
- SSL sites will have 4 variations
You should seem something similar to this:
(source: bybe.net)
Adding a preferred domain in the process takes no more than a few seconds but in additional to 301 and canonicals this option is a great fallback just in case for some reason your 301's or canonical links stop working for one reason or more.