
Moving a WordPress blog across domains using the shell
Here’s a rundown, or more accurately a set of personal notes I made, for moving a WordPress blog from one domain name to another. This will also work when moving from one web host to another.
First you’ll need to know some details of your old database:
- database host name, e.g
mysql.yourdomain.com
orlocalhost
- database name
- username
- password
You can find these details in wp-config.php
on the old WordPress site.
Create a new database on the new web host, and an appropriate user account which can be used to manage the database. You may need to do this in a web interface, e.g cPanel, as some web hosting companies only allow database creation and deletion via a web interface. Make note of these details of the new database, you’ll need these new details for the new site.
We’ll assume here that on your web host, web served files are stored in a subdirectory named the same as the domain name.
Old Blog
Save WordPress files from the old blog to blog_old.tar.gz
:
tar -zcvf blog_old.tar.gz -C OLDDOMAIN.COM .
Save the old database to blog_db_old.sql
. Use the details from the old database here.
mysqldump -uUSERNAME -p -h DBSERVER DBNAME --quick > blog_db_old.sql
Check the database dump is okay, we’ll just read the first couple of lines just to make sure all is well.
head blog_db_old.sql
New blog
Now we get to work on the new site. First up the archive of the WordPress files and database dump, move those onto the new server if required.
Write to new database and WordPress directory
Import the dumped SQL file from the old database, into the new database. You’ll need to type in here the details of the new database that was just created, and use a 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 contents of WordPress install into the new domain.
tar xvf blog_old.tar.gz -C NEWDOMAIN.COM/
Edit wp-config.php
and enter in the new database details.
cd NEWDOMAIN.COM
vim wp-config.php
Search and replace operation on database
The database still references the old domain in its URLs. So we need to replace the domain name and URLs in database. There is a PHP script by interconnectit you can use, as a search and replace tool.
Download this handy 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.
- Select script to obtain database parameters from
wp-config.php
. Use this option, only if you have already editedwp-config.php
on the new site with the new database details. - Select all tables to perform the search and replace in.
- Type your search and replace parameters. Search for your old URLs and replace with your new URL. Take note of
www.
at the start of addresses. Some sites use it and some don’t (normally set in cPanel or equivalent interface). For example when I was moving sites from my old domain to this one, I searched forhttp://micro.gock.net
and replace it withhttp://agock.com
. - Check your web site on the new domain and check all is working fine.
Remove the search and replace script once completed. Don’t leave this script on your web site.
rm sr.php
Alternate method
As an alternate method to using the above PHP script, we could have performed a search and replace on the SQL dump file, or peformed some search and replace commands directly on the databse using SQL queries.
Test web site
After performing the search and replace, the new site should work as normal. All media and user accounts should be as before. Visit your new site and check that it is all working fine.
Redirect the old domain
Once we know the new domain is working okay, we can remove the old files (remember you still backups from earlier). Make sure you’re in the right directory before doing the mass delete!
cd OLDDOMAIN.COM
rm -rf *
Set up a .htaccess
file for the old domain.
touch .htaccess
vim .htaccess
Insert the following but replace with your site domain names:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
Test the redirection, by visiting an old URL, your browser should automatically redirect to the new domain name. This will redirect all pages. Search engines crawling your site should recognise this 301 redirect code, and update its indexes appropriately.
Finishing up
You can now delete the old database as well. This can be done via cPanel or similar web host interface, if that is the only method your web host allows.
Copy your old site archive (.tar.gz) and database dump (.sql) somewhere for safekeeping.
If you were using Google Analytics, remember to go and change the settings in there, you are able to change the site name and URL for a Google Analytics account / property. Also fix any linking with Google Webmaster Tools.