Preferred Domain Redirects (HTTP, HTTPS, www)
Configuring a “preferred” domain is probably one of the most common questions I’ve seen during my years as a WordPress and SEO consult, and it’s also one of the most poorly executed by website owners and web developers alike.
Recall The 4 Possible Domain Variations
The first step here is to understand all the possible variations of a given domain:
…that is, there should be a total of 4 variations for most TLDs, which include 2 HTTP variations and 2 HTTPS variations, with a “www” and “non-www” version for both of these protocols, respectively.
Choose The Variation You Most Prefer
Without getting into too much discussion in this post about which variation is best for which situation, suffice it to say that your company or team needs to decide on a clear “preferred” domain a.k.a. “canonical domain” in some circles.
After you have chosen which variation you prefer, it is important to not change your mind — at least, not too often. The reason is that each of these variations can be seen as a different website domain by companies such as Google who carefully track the reputation, backlinks, and so forth of web properties. Plus, there is the issue of all the links pointing to your website, along with traffic analytics, static resource URLs, and more to worry about. Therefore, changing your preferred domain should be an extremely thoughtful decision, and one that you do NOT change more than once, ideally.
Update Your WordPress Settings, Etc.
Now comes the fun part (heh). After your are SURE about which domain variation you want to keep as your primary domain, you need to update your CMS (WordPress) and database settings accordingly.
Firstly (and more importantly) you should update your “Site URL” and “WordPress URL” as required:
Next, you should scan and replace all instances of the 3 “undesired” variations across your entire MySQL database, and replace them with your preferred variation. Yes, that means that you need to run a minimum of 3 “search and replace” SQL queries. The easiest way to do this is with a plugin, such as Better Search Replace.
After this stage, you have truly cleaned up your domain situation, however, we aren’t done yet…
301 Redirect All Non-Preferred Domains
Here is where things get a little dicey, because there are so many conflicting recommendations out there. In general, I strongly recommend NEVER using a WordPress plugin or any other application-level script (i.e. PHP) to perform 301 redirects in regard to your TLD. In other words, you should only use plugins (etc) to redirect URIs that come AFTER the TLD, such as a page slug.
Rather, dealing with protocol redirects (HTTP + HTTPS) or subdomain / naked domain (www vs. non-www) should take place on the server level, and/or on the DNS level, to make sure for security, stability, speed, and SEO reasons that the 301 redirect is completely solid and cannot be circumvented in the case of an application break down.
If you are using Nginx, our recommended server block rules make this step very easy. If you are using Apache, the most common method is via htaccess although you can also do this directly in your Virtual Host settings.
Lastly, for good measure, you can also perform a permanent 301 redirect at the DNS level by using a free tool such as CloudFlare. Using their Page Rules feature, you can simply introduce a “rule” for each of the 3 undesired domain variations mentioned above, and 301 redirect these along with wildcard variables to your preferred domain variation. In this way, you have a double layer of 301 redirects for your website, making it all but impossible for hackers, bots, or anything else to find its way into one of your undesired domain variations!
NOTE: if you are a LittleBizzy client, all the above is included for just $40 SSL setup!