How to change WordPress main URL to new one

Using WP-CLI (preferred):

Enter folder where your WordPress is installed:

username@server1 [~]# cd ~/public_html

Create backup of your database:

username@server1 [~/public_html]# wp db export

The following command won’t do any change because of –dry-run key specified but show you tables and amount of replacement

username@server1 [~/public_html]# wp search-replace 'OLDdomain.com' 'NEWdomain.com' --dry-run
+------------------+-----------------------+--------------+------+
| Table | Column | Replacements | Type |
+------------------+-----------------------+--------------+------+
| wp_commentmeta | meta_key | 0 | SQL |
| wp_users | user_activation_key | 0 | SQL |
| wp_users | display_name | 0 | SQL |
+------------------+-----------------------+--------------+------+
Success: 5548 replacements to be made.

Run the following to replace:

username@server1 [~/public_html]# wp search-replace 'OLDdomain.com' 'NEWdomain.com'
+------------------+-----------------------+--------------+------+
| Table | Column | Replacements | Type |
+------------------+-----------------------+--------------+------+
| wp_commentmeta | meta_key | 0 | SQL |
| wp_users | user_activation_key | 0 | SQL |
| wp_users | display_name | 0 | SQL |
+------------------+-----------------------+--------------+------+
Success: Made 5548 replacements.

Clear your browser’s cache/cookies and re-test your new URL

Using MySQL replace command if available:

Create MySQL dump:

wp db export

or

mysqldump username_dbname > username_dbname.sql

Create backup of SQL file:

cp -rp username_dbname.sql{,-bk}

Run to replace:

replace 'OLDdomain.com' 'NEWdomain.com' -- username_dbname.sql

Import back updated data:

mysql username_dbname < username_dbname.sql

Clear your browser’s cache/cookies and re-test your new URL

Using vi/vim editor:

Open SQL dump file using vi editor

vi username_dbname.sql

Type as the following:

:%s/OLDdomain\.com/NEWdomain\.com/g

Press Enter on your keyboard to run replacement

To save changes and quit type as the following and press Enter:

:wq!

Depends on the size of your SQL dump file the saving can take some time, so be patient

Import updated data:

mysql username_dbname < username_dbname.sql

Clear your browser’s cache/cookies and re-test your new URL

Similar Posts:

Leave a Reply

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