When we’re developing our websites, we always develop in a local testing environment, so the URL for the website will be something like http://dev.testexample.com. Once the website is ready to deploy onto a live server, we copy everything across- web files and database. So essentially, we’re changing the websites URL, from http://dev.testexample.com to http://www.liveexample.com. However, WordPress won’t “just work” when a migration takes place if the domain name is changed, a little meddling with in the database is required.
First, you might upload all the WordPress files to the live server via FTP, and then you might import the database. Assuming you have the correct database connection details in the wp-config.php file, you might notice the website doesn’t work properly e.g. the pages buy custom papers online redirect to the old test domain and images in blog posts aren’t appearing. The solution to get this resolved is by carefully running a few queries in your newly imported DB.
Step 1: update wp_options table
These queries will set your new domain, so WordPress will no longer redirect to your local/old domain.
Query 1: update ‘siteurl’ field
UPDATE `wp_options` SET option_value = 'http://www.liveexample.com' WHERE option_name = 'siteurl'
Query 2: update ‘home’ field
UPDATE `wp_options` SET option_value = 'http://www.liveexample.com' WHERE option_name = 'home'
Step 2: update wp_posts table
These queries will resolve the missing images problem, and also update any hyperlinks you may have created with in a post/page which contain a link to the local/old server.
Query 1: update ‘post_content’ field
UPDATE `wp_posts` SET `post_content` = REPLACE(`post_content`, 'dev.testexample.com', 'www.liveexample.com');
Query 2: update ‘guid’ field
UPDATE `wp_posts` SET `guid` = REPLACE(`guid`, 'dev.testexample.com', 'www.liveexample.com');
Step 3: update wp_postmeta table
This query will update any custom field type entries, which reference the old domain.
UPDATE `wp_postmeta` SET `meta_value` = REPLACE(`meta_value`, 'dev.testexample.com', 'www.liveexample.com');
If you’re overwriting an existing website which is live, ensure to make a backup! That is crucial.
The steps above are the ones we take when migrating a test version of a website to a live server. It works for us. The queries may not work for every WordPress setup, but it should work for most basic setups.
If you have any other methods of migrating a WordPress environment, we’d love to hear it, including any helpful tips.
Learning & sharing
This is where we store some of our Web Development thoughts, tips and tricks for both our own uses and for others to learn from. Sharing is caring.
Please feel free to contribute to our blog posts; perhaps even teach us a few tricks of your own. We'd love to hear your thoughts.