Migrating a WordPress Website to a New or Different URL / Domain Name

Migrating a WordPress Website to a New or Different URL / Domain Name

When migrating a WordPress website from one URL to another, it's not as simple as exporting the current database to a SQL file, changing the URLs in the SQL file, dropping all tables in the database, and then re-importing the updated SQL file (which contains the updated URL).  Using a find and replace text editor utility to update the old URL to the new one in the MySQL database dump file IS NOT the right way to do it.

Because of how PHP serializes data, which is then stored in the WordPress database, one cannot simply do a find and replace to update the old URL to the new one.  While it will work for the most part, if you have an option table setting that has been serialized and contains the old URL, you will need to deserialize it first and then reserialize it after updating the values.  You can use WordPress' built-in functions in your theme's functions.php file to do that.  Here is an example of what I had to do in my theme's functions.php

$option = get_option('mytheme_option_name');
$option['logo'] = '{NEWURL}/logo.png';
$option['logo_hd'] = '{NEWURL}/logo@2x.png';
$option['favicon'] = '{NEWURL}favicon.png';
update_option( 'mytheme_option_name', $option );

The easiest way to get an existing WordPress site to run on a new URL or domain name is to use the WP-CLI utility.  Unfortunately, this wasn't going to work for me since I was running an old WordPress website on an old server running PHP 5.5.x.  As a result, I had to manually identify the options that needed to be updated and update them in a way that properly deserialized and then serialized the changes properly.

If you are able to run the WP-CLI utility, here is how you can update / change the URL of the existing website to the new one easily and properly.  Be sure to make a full backup of your existing WordPress database using a utility like PHPMyAdmin or via the command line.  Then, use the below commands (update the values from the example first before running them):

wp option update siteurl 'https://yournewdomain.com'
wp option update home 'https://yournewdomain.com'
wp search-replace 'old-url.com' 'new-url.com'

That should do it.

Rent dedicated game servers from Chicago, Kansas City, Dallas Texas, Wilkes-Barre Pennsylvania, Arizona, Denver Colorado, California, Florida, and Sofia Bulgaria starting as low as $7.45 a month. We Be HostiN (https://webehostin.com)

Tags: , , , , , , , , , , , , , , , , , , , , , ,

Leave a Reply

You must be logged in to post a comment.