Change WordPress Domain URL with WP-CLI Tool

There are a few ways to change the main WordPress domain URL of a site – here is how to do it with the WP-CLI tool, which is probably the fastest and less problematic way of doing it.

You’ll need to access your webhost via a shell session over SSH, once there switch directory to your web root folder such as public_html or htdocs

cd public_html

Test to see if you have wp-cli installed

wp-cli --info

You should see output

OS: Linux 4.15.0-46-generic #49-Ubuntu SMP Wed Feb 6 09:33:07 UTC 2019 x86_64
Shell: /bin/bash
PHP binary: /RunCloud/Packages/php74rc/bin/php
PHP version: 7.4.5
php.ini used: /etc/php74rc/php.ini
WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: /home/distinct/webapps/distinctivefeatures
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.4.0

The format for the command is wp search-replace ‘olddomain’ ‘newdomain’

wp search-replace 'olddomain.com' 'newdomain.com'

You can test the command with a dry run before doing it live like so…

wp search-replace 'olddomain.com' 'newdomain.com' --dry-run

Also you can swap http to https

wp search-replace 'http://domain.com' 'https://domain.com'

Take a Database Backup

You may want to take a database back also before you commit to the changes, do so like this

wp db export

This will export the database to the current webroot directory, probably not a good idea to leave it there. It will be named along the lines of…

mydomain_dbname.sql

To import that database back (whilst still in the webroot directory) and override the original use…

wp db import mydomain_dbname.sql

A succesful import will give output…

Success: Imported from 'mydomain_dbname.sql'.

If you can’t get a shell or command line access or don’t have wp-cli there are alternatives you can try such as WP Migrate Pro, Better Search and Replace, interconnectit Search & Replace or manually in phpMyAdmin or MySQL – see a write on these here.

Leave all Comment