Changing the domain name on an existing WordPress installation or staging a WordPress website at a different location may seem daunting at first. But it’s really quite easy to do. And I’ll show you how.
Use case #1:
You’ve purchased a number of domain names for your website. And you wish to assign another of your domain names as the main domain people see in the web browser location bar when they visit your website.
Use case #2:
Your client has asked you to update their existing theme but would prefer you not edit their live website. Staging the website in a subdirectory within the current hosting account will allow you to work with the existing theme in a safe environment.
Use case #3:
You would like to move your website to another web host, but test your website at the new host before pointing the domain name to the new hosting account.
The process for making a backup of a WordPress website in preparation for a move to another hosting account or for staging purposes is rather straightforward.
Start by making a backup of your existing website files and database.
Backing up files and databases within cPanel is as simple as clicking Backup, then clicking two links:
- Download a Home Directory Backup
- then Download a MySQL Database Backup
Once you have a solid backup of files and database:
- Set up a staging subdomain within your web hosting account like staging.at-your-domain.com, or use a different domain name entirely.
- If you are installing your WordPress site within a subdirectory on a cPanel server, click the Addon Domains button.
- If you are installing your WordPress site into the main directory on a cPanel server, click the Aliases button, and you’ll be all set in two clicks.
- Then install WordPress into the new account or directory.
- Rename the wp-config.php file for now.
We are doing this so that when you recover your site files you do not inadvertently overwrite the newly set up wp-config.php file. - Upload and extract your backup of files within to your staging directory.
- Delete the wp-config.php file you just copied over from the live site and rename the wp-config.php file renamed in (3) above back to wp-config.php
- The how to import a database part of the process can be a bit tricky the first time around.
- Since you already have a working wp-config.php in place and a working database in place, use your FTP or your web host’s file editor to view the contents of your wp-config.php in order to obtain the database name.
- Log into your PHPMyAdmin. Click on the database name at left. This will display a list of tables. Check all tables, choose the Drop option, then Go to drop all the tables.*
*All you are doing here is removing the existing database tables in preparation for import. - And finally, click the Import option at the top, Choose File, select that database you downloaded to your computer earlier, then click Go.
- If all goes well with the import you will have completed the initial staging setup process.
If you are setting up a development site using a domain name different than the original, just add these two lines in gray to the top of your wp-config.php.file (below the line “<?php”), example:*
<?php
define('WP_HOME',"http://{$_SERVER['SERVER_NAME']}/"); define('WP_SITEURL',"http://{$_SERVER['SERVER_NAME']}/");
* The WordPress.org website covers editing wp-config.php fairly nicely, in case you are interested in reviewing the technical bits.
Then visit your migrated website.
99% of the time the two lines of text above are all you need to do!
“So what’s this magic going on behind the scenes” you may ask?
Ok, I’ll let the rabbit out of the hat. The guts of your WordPress settings are maintained in the wp_options table. All this magical bit of gray text in #2 above does is override your siteurl settings in your database.
Presto magico!
Below is an example of the above how to migrate your WordPress website to a different domain name.
The original website with website address:
pharm.tvsecure.net
I copied the same website into a subdirectory of the same account for staging purposes, at:
/pharm-staging.tvsecure.net
and set the domain name pharm-staging.tvsecure.net to point to that subdirectory.
However, we had one obvious problem. Because the staging website was copied from pharm.tvsecure.net any attempt to visit pharm-staging.tvsecure.net resulted in an immediate redirect back to the original pharm.tvsecure.net domain.
Well, that’s annoying.
So, following step #2 above, I simply copy/pasted the two lines shown above into the top of my /pharm-staging.tvsecure.net/wp-config.php file, like this:
The result? When I go to pharm-staging.tvsecure.net the new website address pharm-staging.tvsecure.net I had staged earlier within the directory:
/pharm-staging.tvsecure.net appears nicely like this:
Well, that about covers the basics of migrating a WordPress website to a different domain name by copy/pasting two lines of text.
If you have suggestions or additions to this WordPress staging or migration related article please be sure to email me anytime, jim at HackRepair.com
Enjoy!
Everyone is well aware of the fact that, it is always recommended not to deploy/test any changes to a live site.