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).
- First, log into your server via SSH.
- Next, navigate to your WordPress installation. Assuming it's 'example.com', you'd probably run the following:
[server]$ cd ~/example.com
- 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'.
- First, make a backup of your database. Just in case something goes wrong, you can easily replace your old database data.
- Download the Better Search Replace plugin. In your panel navigate to 'Plugins > Add New'.
- Search for 'Better Search Replace'.
- Click Install Now.
- Click Activate.
- In your Dashboard, navigate to the plugin under 'Tools > Better Search Replace'.
- Click the 'Search/Replace' tab.
- In the first field titled 'Search for:', enter your current http URL. For example:
At this point, all URLs in your database point to your site's new URL.