This tutorial is applicable to all types of sites, not only WordPress.
When you install WordPress, you get 2 options to set your website’s primary URL.
You can set the site with something like www.yoursite.com or you can skip the www part and go with yoursite.com.
In both cases, you are generating canonical URLs with duplicate content.
Though it is unlikely to create issues with SEO since Google is smart enough to know that canonical URLs can be unintentional, it makes sense to present uniform URLs to visitors.
The preferred version for any URL is the one with WWW in it. This has been the norm for a long time.
So how to present single URLs for canonical versions of web pages? The answer is 301 redirects.
The 301 redirects can be achieved through rewrite rules in htacccess file.
Download your htaccess file from server and add the following rules in it. Make sure not to put the rules inside BEGIN WordPress and End WordPress tags.
You can put the rules either on top or bottom of these two tags.
RewriteCond %{HTTP_HOST} ^yoursite.com [NC]
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301]
# BEGIN WordPress
...
...
# END WordPress
If you want to do the reverse, that is redirect the URLs from www to non-www version than just reverse the rules as follows
RewriteCond %{HTTP_HOST} ^www.yoursite.com [NC]
RewriteRule ^(.*)$ https://yoursite.com/$1 [L,R=301]
# BEGIN WordPress
...
...
# END WordPress
Also I am assuming that you already have SSL installed on your domain so that all calls to non-secure http URLs are redirected to secure https URLs. If your domain does not have SSL installed then change the https to http in the code above.
Copyright Web/ Design/ Vista 2022 ©