Moving a WordPress blog across domains using the shell

2013-09-24

This article provides a comprehensive guide, or more aptly, a set of personal notes, on how to migrate a WordPress blog from one domain name to another. This process is also applicable when transitioning from one web host to another.

Initially, you need to gather some details about your old database:

  • Database host name (e.g., mysql.yourdomain.com or localhost)
  • Database name
  • Username
  • Password

These details can be found in the wp-config.php file on your old WordPress site.

Next, create a new database on your new web host and establish an appropriate user account to manage this database. This step may need to be performed via a web interface, such as cPanel, as some web hosting companies only permit database creation and deletion through such an interface. Remember to note down the details of the new database, as you will need them for the new site.

For the purpose of this guide, we will assume that on your web host, web-served files are stored in a subdirectory named identically to the domain name.

Old Blog

Archive the WordPress files from the old blog into blog_old.tar.gz:

tar -zcvf blog_old.tar.gz -C OLDDOMAIN.COM .

Backup the old database to blog_db_old.sql using the details from the old database:

mysqldump -uUSERNAME -p -h DBSERVER DBNAME --quick > blog_db_old.sql

Verify the database dump by reading the first few lines:

head blog_db_old.sql

New Blog

Now, let's focus on the new site. First, move the archive of the WordPress files and the database dump onto the new server, if necessary.

Write to New Database and WordPress Directory

Import the dumped SQL file from the old database into the new one. Here, you'll need to input the details of the newly created database and use an appropriate path to the archive files (the .tar.gz and .sql files).

mysql -h DB_SERVER -uUSERNAME -p DB_NAME < blog_db_old.sql

Extract the contents of the WordPress installation into the new domain:

tar xvf blog_old.tar.gz -C NEWDOMAIN.COM/

Edit wp-config.php and input the new database details:

cd NEWDOMAIN.COM
vim wp-config.php

Search and Replace Operation on Database

The database still contains references to the old domain in its URLs. Therefore, we need to replace the domain name and URLs in the database. A PHP script by interconnectit can be used as a search and replace tool.

Download this useful database search and replace tool, unzip the single PHP file, and rename it:

wget https://www.interconnectit.com/wp-content/uploads/2011/05/searchreplacedb21.zip
unzip searchreplacedb21.zip
mv searchreplacedb2.php sr.php

Visit your new domain http://NEWDOMAIN.COM/sr.php to run the search and replace script.

  1. Select the script to obtain database parameters from wp-config.php. Use this option only if you have already edited wp-config.php on the new site with the new database details.
  2. Select all tables to perform the search and replace in.
  3. Input your search and replace parameters. Search for your old URLs and replace them with your new URL. Be aware of www. at the start of addresses. Some sites use it, and some don't (normally set in cPanel or an equivalent interface). For example, when I was moving sites from my old domain to this one, I searched for http://micro.gock.net and replaced it with http://agock.com.
  4. Check your website on the new domain and verify that everything is functioning correctly.

Once completed, remove the search and replace script. Do not leave this script on your website.

rm sr.php

Alternate Method

As an alternative to using the above PHP script, we could have performed a search and replace on the SQL dump file or executed some search and replace commands directly on the database using SQL queries.

Test Website

After performing the search and replace, the new site should operate as normal. All media and user accounts should be as before. Visit your new site and check that everything is functioning correctly.

Redirecting the Old Domain

Once the new domain has been confirmed to be functioning correctly, the old files can be safely removed. Remember, you should still have backups from earlier steps. Ensure that you are in the correct directory before initiating the mass deletion:

cd OLDDOMAIN.COM
rm -rf *

Next, establish a .htaccess file for the old domain:

touch .htaccess
vim .htaccess

Incorporate the following lines, substituting your specific domain names:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]

To test the redirection, visit an old URL. Your browser should automatically redirect to the new domain name. This will redirect all pages. Search engines crawling your site should recognize this 301 redirect code and update their indexes accordingly.

Finalizing the Process

At this point, you can also delete the old database. This can be accomplished via cPanel or a similar web host interface, if that is the only method your web host permits.

Ensure that you store your old site archive (.tar.gz) and database dump (.sql) in a secure location for future reference.

If you were utilizing Google Analytics, remember to update the settings therein. You can change the site name and URL for a Google Analytics account/property. Also, ensure that any linking with Google Webmaster Tools is corrected.