Saturday, March 24, 2018

Clone or Migrate Wordpress to a Different Domain Name & Fix Static Links




Some Wordpress themes and plugins write the full URL of a website inside their temporary files or database which makes it hard to change the domain name of a wordpress Installation.

In this tutorial, I'll clone Wordpress from xyz.com to abc.com/wordpress


This tutorial will cover the following 3 steps:
1- Clone or Migrate Wordpress
2- Fix Static Links in files
3- Fix Links and serialized strings in database


1- Clone or Migrate Wordpress

There are several ways to do this, you can
- Download all the files and databases and re-upload them to the new server:
Just download the files from cPanel or FTP and the database from PhpMyAdmin and re-upload to new server

- Or Transfer from a server to another using SSH without downloading/uploading
On xyz.com server
Browse to the directory where wordpress is installed and run
mysqldump -u dbuser -p dbtable > db.sql

where dbuser is the db user name (usually found in wp.config)
dbtable is the table name
db.sql is a text file containing all your database tables and data

zip -r wp.zip * .htaccess
This will zip/archive all the files, the database files you have extracted and the .htaccess files (which are needed if your webserver is apache, litespeed..)

On abc.com/wordpress server
Login to your new server/host using SSH, go to the directory where you want to install wordpress and run the commands
wget xyz.com/wp.zip
This will download the files you have previously zipped/archived in the previous step

unzip wp.zip
to extract all the files and db

mysql -u dbuser -p dbtable < db.sql
where dbuser is the database user you have created on the new server
dbtable is the table name you have created on the new server
db.sql is the file you have extracted from wp.zip which was on the old server

vi wp-config.php
and modify the database credentials for the new server (user, table, password)



Make sure to delete db.sql and wp.zip from both servers
rm db.sql wp.zip


- Or Copy files if both websites are on the same server and you have SSH access (you need root access if they are on a different domain name)

On xyz.com server
Browse to the directory where wordpress is installed and run

mysqldump -u dbuser -p dbtable > db.sql
where dbuser is the db user name (usually found in wp.config)
dbtable is the table name
db.sql is a text file containing all your database table

cp -R * /home/yourpath/yourpath
cp .htaccess /home/yourpath/yourpath
Where /home/yourpath/yourpath is the path of your new installtion/clone


On abc.com/wordpress server
mysql -u dbuser -p dbtable < db.sql
where dbuser is the database user
dbtable is the new table name
db.sql is the database file

vi wp-config.php
And modify the database credentials for the new server (user, table, password), you can also do it from FTP


2- Fix Static Links in files

This will be done from SSH

On abc.com/wordpress installation, run in SSH
find -name \*.php -exec sed -i  "s;xyz.com;abc.com/wordpress;g" {} \;
find -name \*.js -exec sed -i  "s;xyz.com;abc.com/wordpress;g" {} \;
find -name \*.css -exec sed -i  "s;xyz.com;abc.com/wordpress;g" {} \;


Make sure to delete db.sql
rm db.sql

3- Fix Links and serialized strings in database

The easiest way is to do it using a php application called search and replace.
Download it from
https://interconnectit.com/products/search-and-replace-for-wordpress-databases/

Extract and upload it to a folder in your wordpress installation, example
abc.com/wordpress/searchr

and open it from your browser, it should detect the database information from wp-config, make sure they are correct

Enter the domain name you want to replace and the new value

You can make a dry run to check which tables will be affected.


Once you are ready do a live run.

Once live run is done, delete search and replace, you can either delete the directory you uploaded or delete it from its interface.