Moving a WordPress Install to a New Domain

Over the years in both my professional and personal lives I have had to move WordPress sites to new homes that required changing the domains. This has led me to develop a set of instructions that I use successfully every time I migrate.

The first warning I should give is that what follows is not the only things you have to do but it is a decent starting point.

Updating the Database

The second warning is that here be dragons. The following requires making direct changes to the WordPress database which could lead to your site no longer working. You have been warned!

The issue WordPress has is that within the database are, depending on the size and age of your site, likely to be lots of references to you current domain and these need to be adjusted.

The following SQL snippet will do the majority of the changes needed. In fact in my experience once this has been run and the domain changed to point to your site it will almost certainly work. However, they may well also be some references to links that haven’t been covered by this.

UPDATE wp_posts SET `post_content` = REPLACE (`post_content`, 'https://olddomain.com/', 'https://newdomain/');
UPDATE `wp_options` SET `option_value` = 'https://newdomain.com/'
WHERE `option_name` = 'siteurl';
UPDATE `wp_options` SET `option_value` = 'https://newdomain.com/'
WHERE `option_name` = 'home';
UPDATE wp_posts SET `guid` = REPLACE (`guid`, 'https://olddomain.com/', 'https://newdomain.com/');
UPDATE wp_posts SET `guid` = REPLACE (`guid`, 'https://olddomain.com/', 'https://newdomain.com/');

One thing to check is whether the domains have trailing slashes and do you have any domains listed as http rather than https – this will depend perhaps on the age of your installation. Have a look in the wp_posts table in particular to check and adjust the SQL above accordingly or run it multiple times.

Other Database Considerations

One area that the snippets above won’t cover are any references that are in PHP serialized format, usually created by a theme editor such as Elementor. They are going to look something like this:

O:8:"stdClass":3:{s:9:"lastCheck";i:123456789;s:14:"checkedVersion";s:5:"2.2.1";s:6:"update";O:8:"stdClass":9:
{s:2:"id";i:0;s:4:"slug";s:15:"softaculous-pro";s:7:"version";s:5:"2.2.1";s:8:"homepage";s:19:"https://
softwp.net/";s:6:"tested";s:5:"6.7.2";s:12:"download_url";s:135:"https://s5.softaculous.com/a/softwp/
download.php?version=2.2.1&license=SOFTWP-12345-12345-12345-12345&url=https://
olddomain.com";s:14:"upgrade_notice";N;s:8:"filename";s:35:"softaculous-pro/softaculous-
pro.php";s:12:"translations";a:0:{}}}

DO NOT RUN SQL ON THESE!!! You must do any changes to these through the front end interface if you want them to continue to work.

Adding Redirects

The final thing that you might want to look at is redirecting any page from the old site to the new if necessary. You can do this with a plugin but I prefer to do it in the WordPress .htaccess file. To do this open the file and add a Redirect 301 line for each redirect.

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
Redirect 301 /index.html /
Redirect 301 /membership.html /membership/
Redirect 301 /diary.html /events/
Redirect 301 /social_events.html /events/
Redirect 301 /news.html /news/
Redirect 301 /groups.html /groups/
Redirect 301 /useful_info.html /news/
Redirect 301 /committee.html /policies-and-procedures/
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Hopefully, the above will help as a starting point for anyone that needs to move a WordPress site.

Leave a Reply

Your email address will not be published. Required fields are marked *