So, you’ve launched your WordPress site on a non-www domain, like example.com, but since then found out that running it on on www, like www.example.com, is better and want to move? You’re in luck, because it is really easy.
Oh, if you haven’t figured out why www is better yet, check out my post “To www or not to www – Should you use www or not in your domain?”
Moving from non-www to www: Step by step
Read through these steps, before you follow them closely, step by step. Don’t miss a single one of them, and you’ll be perfectly fine – both for now and in the future.
Step 1: Disable current redirects
If you have setup any redirects from www to non-www, disable them now. Don’t setup redirects in the other directions just yet. Let’s have your website work on both domains during the transition.
Step 2: Backup your database
Next, backup your database. Yes. Do it. Now. If you don’t know how, install WP-CLI (if you haven’t already) and run this command:
$ wp db export --add-drop-table
Step 3: Analyze your database
Take a look at your database. See what’s in the values in the *_options table, the values in all the fields in the *_posts table and in the value field in the *_postmeta table. Maybe you even have some values in other *meta tables too?
Step 4: Realize you’ll mess this up
Sometimes a simple UPDATE SQL statement with the REPLACE() function on each of those fields will do. At other times, that will corrupt your data since some of those fields contains serialized data. It is your job to look through them and fix them accordingly. One little mistake here and you can mess things up really bad. You might even mess up things that you won’t discover until you’ve finished a backup rotation cycle or done so many other changes that you can’t easily merge the data.
Step 5: Do it with WP-CLI instead
Now, instead of doing anything with that database, install WP-CLI (if you haven’t already) and run this command:
$ wp search-replace 'https://example.com' 'https://www.example.com'
If you somehow messed up your database, revert the effups by running:
$ wp db import
Step 6: Fund WP-CLI
Now, consider how much time and hassle WP-CLI has saved you, and go support the development of WP-CLI financially. Value both the time saved from replacing values in the databse, the comfort of knowing that a professional did this for you, the delightful feeling of knowing you did not screw the site up and the relief of the stressful you never got into.
Step 7: Setup redirections
Finally, setup redirections from your non-www domain to your www domain. Here is how to setup redirections in Nginx.
Will this affect your SEO?
From an SEO perspective, using www or not does not affect your SEO at at all. Transitioning from one domain to another will usually give you a temporary setback. Transitioning between www and non-www: I don’t know. I’m guessing not. I you do know for sure, please leave a comment.