How to Safely Move Your WordPress Site (Without Losing Anything!)
Moving WordPress can seem like a challenging task and one easily placed in the too-hard basket.
The WordPress Codex offers a detailed guide, though it’s confusing at best and doesn’t offer an easy guide.
Whether you’re tired of your current web host and want to move to a new one, or you want to just move your site to the root directory of your domain, migrations doesn’t have to be a headache – and you don’t have to fear making a mistake and blowing up your site.
In this step-by-step guide I’ll cover two common scenarios:
- Moving a website from a subdirectory of a domain to the root directory.
- Moving a website from one web host to another.
Moving a WordPress Site from a Subdirectory to the Root
This is a straightforward method that will allow you to keep your WordPress install in a subdirectory and use your root domain’s URL.
1. Back Up Your Site
It’s a good idea to have a backup, just in case anything goes wrong. This includes your theme files, plugins, any media and your database.
You can use any back up tool you feel most comfortable with. You may choose to copy your files to your computer manually using FTP or SFTP with FileZilla, a backup plugin like WordPress Backup to Dropbox or a third-party service like VaultPress.
You’ll need to ensure you copy any hidden files like .htaccess.
2. Delete Any Existing Site
If there’s an existing site in the root directory, you’ll need to remove it. If you want to keep a copy, now is the time to back it up.
If you have access to Softalucous in your site’s cPanel, you can easily uninstall WordPress from the root directory.
Otherwise, login to phpMyAdmin and drop the database. Then delete all WordPress files.
3. Turn Off Pretty Permalinks
Login to your site and go to Settings > Permalinks and turn off pretty permalinks by selecting the “Default” setting. Save changes.
4. Change Your Site Address
Go to Settings > General and change the address of your site, but not the address of WordPress. So if your site is example.com/test, change your settings to the following:
- WordPress Address (URL): http://example.com/test
- Site Address (URL): http://example.com
Save your changes, but don’t refresh your site just yet.
5. Copy and Edit Files
Login to your site using cPanel or FTP/SFTP and download two files to your computer:
- .htaccess – only if you have one. If you don’t, there’s no need to create one.
Open index.php. Look for the following line:
And replace it with this:
So for our site, we would change the line to:
Save index.php. Now, upload both index.php and .htaccess to the root directory of your site.
6. Turn On Pretty Permalinks
Go back to Settings > Permalinks and turn pretty permalinks back on again with the settings you need for your site.
7. Test Your Site
When you enter the root domain of your site into your browser, the WordPress install in your subdirectory will display, but with the root URL.
And that’s all you need to do to!
Moving a WordPress Website Between Hosts
This is a bit trickier than moving your site from a subdirectory to the root directory. This time we’ll actually need to move some files around.
1. Back Up Your Site
Rather than back up your site in case anything goes awry, you will need to back up files so we can copy them to the new server.
For this step, it’s best to use FTP/SFTP and do things manually. Connect to your old site and copy all of your files to a folder on your computer. Don’t forget to include any hidden files like .htaccess.
2. Export Database
Login to your site’s cPanel and go to phpMyAdmin. Select the database for your WordPress site and then click “Export” at the top of the page.
Leave the default “Quick” option selected and click “Go” to download the database to your computer.
3. Create WordPress Database On New Host Server
The next step is to create a database on the new server for your WordPress site.
In cPanel, go to MySQL Databases and:
- Create a new database.
- Create a new MySQL user.
- Add user to the new database and grant “All Privileges”.
Don’t forget to write down the database name, username and password for later.
4. Edit wp-config.php
Since the database information will be different on the new server, we need to update our site’s wp-config.php file with the new details.
Amongst the files in your WordPress install, which you downloaded earlier, you’ll find a wp-config.php file. Make a copy of this file and rename it wp-config-old.php and save it elsewhere on your computer. This is just in case you make a mistake and need to restore the file.
Open the original file. You will need to update the database name, database username and database password so grab those details from the previous step.
Find the line:
The db_name part of this line will be the new name of your database. Replace this with the name of the database you created earlier.
Change the db_user part of this line to the username you created.
Lastly, track down:
Replace db_password with the password you created.
Save wp-config.php and close it.
5. Import Database
Now that the database is up and running, we need to import the .SQL file we exported earlier.
Open phpMyAdmin in cPanel and select your new database. Click the “Import” tab at the top of the page
Under “File to Import”, select the database file you downloaded earlier.
In the “Partial Import” section, un-check “Allow the interruption of an import in case the script detects it is close to the PHP timeout limit” and then click “Go.” After a little while, depending on the size of your file, you’ll get a confirmation message that the file has been successfully imported.
6. Upload WordPress Files to New Server
We’re almost there. The next step is to upload your site to your new host.
Login to your site using FTP/SFTP and navigate to where you want to copy across your site. If you want your site in the root directory of your site’s domain, then copy your files to the public_html folder.
Leave the copy of your site on your computer for the time being so you’ve got a backup.
7. Edit Database
In order for images and posts linking to other parts of your site to work, you’ll need to update your domain name in your database.
It’s no good just opening up your database and updating it manually because you’ll break any serialized data. A great script for this is Search Replace DB. This script preserves serialized PHP strings when performing a search and replace.
To use the script, upload it to your site, scan all the tables and then input what you want to find (your old domain name) and replace (your new domain name).
8. Configure DNS Settings
Before your site will work with your domain name, you will need to reconfigure your domain’s DNS settings. You will need to login to the site hosting your domain and point your domain to your new web host.
This step is different for every web host, so it’s best that you contact your host if you’re not sure what to do.
9. Enjoy Your New Host
After your DNS changes have fully propagated (this can take up to 48 hours), you will be able to login to your WordPress site on your new server with the username and password you used on your old server.
Moving your WordPress site is a simple and straightforward process, though it’s best to take care with each step and ensure you’ve backed up your site in case anything goes wrong.
If you really can’t be bothered going to the trouble of moving your site yourself, there are plugins that can do all the leg work for you. I’ll explore these options in a post next week, so stay tuned.
Have you ever moved a WordPress site and something has gone horribly wrong? Share your horror story in the comments below.