Change URL of a WordPress website

To change the URL of a WordPress site – for example, when a completely different domain should be used or to change from http:// to https:// – there are various solutions. This post describes two of them.

Important info before starting

  • The new domain should already point to the folder of the installation, so you can access it after making the change. Otherwise, you will not be able to access your website after changing the URL (except it was only a protocol change, then you will get a safety warning from your browser if you do not have a valid SSL certificate).
  • Create a backup, just for safety’s sake.
  • Do not modify the guid column in the database (that would lead to old articles being marked as new in feedreaders).

Modifying the URL with a plugin

There are different plugins that let you change the URL in the database. I like to use Better Search Replace. After installing it, you will find the new menu item ToolsBetter Search Replace. There you can specify what string to replace with what – the following screenshot shows that for the change of my .de domain to the .com domain.

The URL of a WordPress site can be changed using the Better Search Replace plugin fairly easy. (Screenshot: own installation)

Under Select Tables, you can define the tables where the domain should be searched and replaced – generally speaking, you should select all here. By default, the Run as dry run? option is enabled, so you can see how many occurrences would be replaced by the plugin without really changing something. If the result of the dry run looks good, uncheck the option and restart the search and replace.

After that, you should have finished the domain change.

Changing the URL with WP-CLI

If you have installed WP-CLI on your server, you do not need an extra plugin but can use the wp search-replace command. If you are in the installation directory on the command line, that could look like this:

wp search-replace --skip-columns=guid
Code language: Bash (bash)

To make a dry run, add the --dry-run option to the call:

wp search-replace --skip-columns=guid --dry-run
Code language: Bash (bash)

And if you would like to modify all tables of a multisite installation, use the --network option:

wp search-replace --skip-columns=guid --network
Code language: Bash (bash)

If you accidentally changed the domain to something wrong

If you made a mistake while changing the URL, you are likely not able to get into the backend to correct the mistake. In that case, you need SFTP access to your server and can reset the domain with the following to lines in the wp-config.php file:

define( 'WP_HOME', '' ); define( 'WP_SITEURL', '' );
Code language: PHP (php)

That is possible via the WP-CLI, too. Here you can directly update the option in the settings:

wp option update home wp option update siteurl
Code language: Bash (bash)

Or set the constants in wp-config.php (--type=constant is necessary to let the CLI create the entry if it does not exist):

wp config set WP_HOME --type=constant wp config set WP_SITEURL --type=constant
Code language: Bash (bash)

After that, you should be able to get back into the backend. You see: it is also possible to change the domain with one of those three ways, or SettingsGeneral. But in these cases, the domain would not be replaced in the database (or at least not all occurrences, if you change it via the settings screen or wp option update). So in my opinion, the best solution is to use a plugin or the wp search-replace command.

Leave a Reply

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