How to change domain/site url of WordPress?

Last week I ordered new domain called hintdesk.com for my website and I would like to move my WordPress blog system completely to this new domain. You’ll find a lot of articles and instructions in internet for this problem but in this small blog, I would like to illustrate a much more simple method to do that without breaking down the current blog system. The concept can be applied for WordPress but also for any other system and is pretty performant because of taking advantage of server resources.

To keep the migration smoothly, I decide to make a duplicate of old blog and redirect all links from old to new one as following steps.

1. Copying all files from old-domain-folder (assigned for old domain) to new-domain-folder (assigned for new domain). If you host your new domain on same host and your hosting provider has cPanel to use, then use its file manager to copy files, just select all and copy them. It’s comfortable and fast. Using ftp or reupload .zip files is not a good idea because it is waste of time for downloading and uploading data. If you have static links (which are not generated by WordPress), you must be sure that the folders which these links point to, have same structures as before.

2. When copy progress is finished, browse to new domain in web browser, you must see your blog appeared. However, when you hover mouse over any link, you will see that the link still point to you old blog. That’s correct because the new blog system is still using the old database but we are sure that copy progress is successful.

3. In next step, we will change all old-domain-links in database into new-domain-links. Instead of using editor to edit links in database file which is too time-consuming for big database, we’ll take advantage of available programs in server and its resources which is generally faster thanks to powerful hardware of server. However you’ll need SSH privileges because we’ll connect directly to host and execute programs there. For Bluehost users, you can find instructions for connecting to host with SSH at this link http://hintdesk.com/how-to-use-ssh-of-bluehost-with-putty/

4. In your cPanel, create a new blank database for new domain name and build SSH connection to your host. After logging in, let’s dump your old-domain-database as following syntax

mysqldump -u your_username_for_database -p old_domain_database > dummy_name.sql

You’ll be prompt for entering password. Please note that this is the password of username for database, not the password for logging into host.

5. Rename all old-domain-links to new domain with following syntax

sed 's/old_domain/new_domain/g' dummy_name.sql > new_dummy_name.sql

If your blog is not in top domain like mines (subdomain, subfolder) such as http://rongchaua.net/blog then the syntax should be

sed 's/rongchaua.net\/blog/new_domain/g' dummy_name.sql > new_dummy_name.sql
sed 's/rongchaua.net/new_domain/g' dummy_name.sql > new_dummy_name.sql

I must execute replace command twice, one for subdomain and one for topdomain so that all links generated by WordPress or static links will be replaced correctly.

6. Now we’ll restored modified dump back to new domain database created before as following syntax

mysql -u your_username_for_database -p new_domain_database < new_dummy_name.sql

You’ll be prompted for password. Enter password of database user to finish.

7. Go to folder of new domain, edit file wp-config.php to connect it to your new database. In your web browser, go to your new domain. Everything is now ok. All links works and points exactly to new domain name.

8. Of course we still need to redirect all traffic from old one to new one. It’s pretty simple, go to top folder of your old domain, and create an .htaccess file with following redirect rules

RewriteCond %{HTTP_HOST} ^rongchaua\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.rongchaua\.net$
RewriteRule ^blog\/?(.*)$ "http\:\/\/hintdesk\.com\/$1" [R=301,L]

Now we’re finished the migration. It’s simple and fast thanks to taking advantage of server resources.

Leave a Reply

Your email address will not be published. Required fields are marked *