Changing all URLs in your WordPress database

WordPress hardcodes the site's domain name into the database in such things as image and link urls. This is a problem if you migrate the site to another domain, because when you click on a link on your new site, it redirects back to the old site.

To update the URLs in your database, you can use a plugin or command line

Option 1 — Using a plugin

Below are a couple of plugins that work to change all URLs in your database.

Option 2 — Using the command line

If you are familiar with the command line, you can run the following command for the same effect (assuming you saved the old WordPress address).

  1. First, log into your server via SSH.
  2. Next, navigate to your WordPress installation. Assuming it's 'example.com', you'd probably run the following:
    [server]$ cd ~/example.com
  3. In your site's directory, you can use wp-cli to update the URLs using the following command.
    [server]$ wp search-replace http://example.com http://new.example.com --dry-run

The --dry-run call at the end tests the address

If the changes make sense to you, then run the above command again without the 'dry-run' call.

At this point, all URLs within your WordPress site will use the new domain name.

Full example using the Better Search & Replace plugin

These steps walk you through how to update your URLs using the 'Search & Replace' plugin. This example changes the URLs in the database from 'example.com' to 'example.com/blog'.

  1. First, make a backup of your database. Just in case something goes wrong, you can easily replace your old database data.
  2. Download the Better Search Replace plugin. In your panel navigate to 'Plugins > Add New'.
  3. Search for 'Better Search Replace'.
  4. Click Install Now.
  5. Click Activate
  6. In your Dashboard, navigate to the plugin under 'Tools > Better Search Replace'.
  7. Click the 'Search/Replace' tab.
  8. In the first field titled 'Search for:', enter your current http URL. For example:
    • https://example.com
  9. In the second field titled 'Replace with:', enter your new URL. For example:
    • https://example.com/blog
  10. Select all tables.
  11. Check the box titled 'Run as dry run?'. 
  12. Click Run Search/Replace.
  13. This should respond with a message at the top notifying you of what would have been updated if you did not check the 'Dry Run' box.
  14. If everything looks good, run it again with the 'Run as dry run?' box UN-checked.
  15. A confirmation will display notifying you of how many changes were made.

At this point, all URLs in your database point to your site's new URL.

See also

Did this article answer your questions?

Article last updated PST.