This page looks best with JavaScript enabled

Migrating Wordpress Sites Across Domains

 ·   ·  ☕ 7 min read

I recently had to pick up an old project on Wordpress and deploy the site on a CPanel server. So, I thought it was just the right time to document the steps needed to migrate Wordpress site from one host to the other, or from one domain to the other. While there are references to CPanel, the migration is almost the same for any control panels (or even when you don’t use one).

The Art of Wordpress Site Migration

It may be just me but I don’t quite understand why Wordpress site migration has to be so “difficult” for a typical user. Note that I said “difficult” as compared to other user-facing, ‘citizen-admin friendly’ platforms. It is indeed cake-walk when compared to typical developer-owned tasks. I may not exactly an expert to deep dive and show why things are as they are but, there are more than few ways of making the process easy.

Before we go further on the topic, let’s look at typical use cases for migrating Wordpress sites -

  1. Migrate sites from one host to the other
    The movement of sites from one host to the other result in different database names, connection parameters, and file structures.
  2. Migrate sites from one domain to the other
    This refers to moving site from one domain (say example.com) to a new domain (newexample.com), or migrating from a test to a production system (example.test to example.com).

We don’t have cover all use cases to keep this post sane, instead we focus on “typical” migration cases that one (me!) is most likely to do.

Here are the typical questions when migrating sites -

  1. Which components need to be migrated?
  2. How should we move data/files/other?
  3. What options do I have for Wordpress site migration, and which is the best option?

I fussed with such questions over years, used different workflows, and finally, developed one plugin-independent flow that simply works. The workflow may not be optimal (let me know!), or may be even wrong - but it does get the work done. So, YMMV.

This flow is more geared towards people who are comfortable working with databases, applications and so forth. Here are the pre-requisites -

  • Database created in the target environment. User id /password and relevant host details are available to you
  • File system access (e.g. through an explorer in CPanel) available to you

Components for Migration

Wordpress is based on PHP, and like many PHP applications, is quite simple at heart. We primarily deal with two components -

  1. Files
  2. Database

Files

Files reside in wp-content folder within Wordpress root installation folder. They may include -

  1. User data including images, video and other media, data files that you may be using in your site, files made available for download, and other user generated files.
  2. Site customisation files - themes, styles, etc.

Database

Export database from the source and import in the target database.

You can change the database to reflect the new site in a one of the below ways -

  1. Change strings in export file before importing content in target
  2. Run SQL in target database after the import
  3. Run a PHP script that will do the updates for you

Workflow for Wordpress Site Migration

This workflow is generic in nature and works for typical migration use cases -

  1. Moving sites from one host to the other
  2. Moving sites across environments (e.g. from test to production)

Copy Files

All you are expected to do is copy the entire Wordpress folder from the source to the destination. You could also copy/paste wp-content on the same versions of Wordpress with similar configuration.

Once the files are copied over, you do a few changes -

  1. Update configuration (for e.g. in wp-config) to point to the right database. Your old file will have the older database details. Change -

    • Database host and/or database name (CPanel may enforce naming convention of prefixing table names with your root domain. e.g. techformist_wp_options)
    • User id / password
  2. Revert .htaccess file in the root folder to the default. You can also regenerate this file from within Wordpress. In its simplest form, .htaccess looks like the below.

    # BEGIN WordPress
    
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    # END WordPress
    

Export and import database

We do this database change to migrate Wordpress sites across domains. If you have the exact same domain (with HTTPS) - skip this step.

Login to your source environment. Export database to a file in source.

If you are using CPanel, go to phpMyAdmin from your control panel home. Select your database and go to Export tab.

wordpress-migration-export-database-cpanel

Defaults will work for most cases, just click Go to export database to a file. This will export the table structure and data.

If you are migrating to an existing database and want to overwrite target with data from the source, explore the options to delete and re-create tables -

  • select the Export method as Custom - display all possible options
  • then select Add DROP DATABASE IF EXISTS statement option.

You can import the file in target database as-is, but it may make sense to change the export file before you import. This is particularly useful if you are changing your domain name or migrating from test to production (also a domain name change).

  1. Open the file in an editor like Notepad++
  2. Hit Find and Replace
  3. Find for all instances of oldurl and replace them with newurl
  4. If the new url is not setup yet, it is a good idea to replace with http version of the URL rather than using https

To import the file -

  1. Logon to your phpMyAdmin on target host
  2. Navigate to Import tab
  3. Select the file to import and hit Go

[Optional] Updating database without file modifications

Again, this step is relevant only if you are changing domain names.

If you cannot / do not want to work with files, you can always work with SQL to update database to work in your new setup.

First, import the files in target database using phpMyAdmin or similar tools.

In phpMyAdmin, go to SQL tab. Paste the following SQLs .

1
2
3
4
UPDATE wp_options SET option_value = replace(option_value, 'oldurl', 'newurl') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'oldurl','newurl');
UPDATE wp_posts SET post_content = replace(post_content, 'oldurl', 'newurl');
COMMIT;

Replace oldurl and newurl with your relevant URLs from the source and target systems. Hit Go.

Finishing up

Provision SSL certificates for your target site. Once again, this is made easy by control panels.

In CPanel -

  1. Go to Let's Encrypt link on the home page of control panel
  2. Select Install Certificate against your domain

In a VPS just install Certbot, and it will do the rest for you :).

Update your site to use https-

  1. Login to /wp-admin
  2. Go to Settings > Select General
  3. Change WordPress Address (URL) and Site Address (URL) to use https. Click on Save Changes. This will throw an error message that you can disregard
  4. Re-login to wp-admin using the new https URL. Go to Settings > Select Permalink from menu
  5. Under Common Settings change the URL setting to any option and revert back to your desired option

Alternatives

The workflow works for me because I am not afraid of databases and servers. I also do not work on Wordpress actively and may be do < 5 migrations per year.

There are alternative solutions that are more elegant, but have dependencies on third party tools.

Plugins

There are plugins that you could use to automate the tasks in part or fully.

  1. Updraft Plus allows you to backup database and files from the source. You can “restore” the content in the destination. Updraft plus handles many complexities including updating the new host names, etc.
  2. WP Migrate DB takes care of exporting databases while allowing you to find and replace content within the database. The pro version enables you to work with files as well

Despite using plugins, you still have to do the backend configuration work including -

  1. Updating wp-config to point to the new database and new file system (you may skip this step if you use Wordpress installation done by someone else or by one-click installers)
  2. .htaccess updates

CPanel Migration

When you are on CPanel and changing hosts, check with your new hosting company whether they support free migrations. CPanel allows your hosting company to transfer from an existing host without any fuss - this is an one-time migration.

The process requires no effort from your end. Just buy new hosting, open a ticket for migration, and you are done. Your site will continue to function without problems.

Similar solutions may exist for other control panels too.

You are done!

Enjoy your new site.

Stay in touch!
Share on

Prashanth Krishnamurthy
WRITTEN BY
Prashanth Krishnamurthy
Technologist | Creator of Things