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 -
- 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. - 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 -
- Which components need to be migrated?
- How should we move data/files/other?
- 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 -
- Files
- Database
Files
Files reside in wp-content
folder within Wordpress root installation folder. They may include -
- 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.
- 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 -
- Change strings in export file before importing content in target
- Run SQL in target database after the import
- 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 -
- Moving sites from one host to the other
- 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 -
-
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
- Database host and/or database name (CPanel may enforce naming convention of prefixing table names with your root domain. e.g.
-
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.
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
asCustom - 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).
- Open the file in an editor like Notepad++
- Hit
Find and Replace
- Find for all instances of
oldurl
and replace them withnewurl
- If the new url is not setup yet, it is a good idea to replace with
http
version of the URL rather than usinghttps
To import the file -
- Logon to your
phpMyAdmin
on target host - Navigate to
Import
tab - 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 .
|
|
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 -
- Go to
Let's Encrypt
link on the home page of control panel - 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
-
- Login to
/wp-admin - Go to
Settings
> SelectGeneral
- Change
WordPress Address (URL)
andSite Address (URL)
to usehttps
. Click onSave Changes
. This will throw an error message that you can disregard - Re-login to
wp-admin
using the newhttps
URL. Go toSettings
> SelectPermalink
from menu - 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.
- 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.
- 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 -
- 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) .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.