How to Update WordPress URLs

When changing your WordPress site's domain name or moving it from a staging site to production, updating URLs is essential to ensure all links and resources point to the correct location. Here are two methods you can use: one with a plugin and one using WP-CLI for command-line users.

Take a backup of your database before you start. Both methods below rewrite your whole database and neither can be undone.

Use one of these two tools rather than a find-and-replace in phpMyAdmin. WordPress explains why: "If you do a search and replace on your entire database to change the URLs, you can cause issues with data serialization, due to the fact that some themes and widgets store values with the length of your URL marked. When this changes, things break." Both tools below understand serialized data. A raw SQL query does not.

Option 1: Using the Better Search Replace Plugin

  1. Install the Plugin
    • In your WordPress dashboard, go to Plugins > Add New.
    • Search for Better Search Replace. (Manual Install Link)
    • Install and activate the plugin.
  2. Run a Search and Replace
    • After activation, go to Tools > Better Search Replace.
    • In the Search for field, enter your old URL (e.g., staging.example.co.uk).
    • In the Replace with field, enter the new URL (e.g., example.co.uk).
    • Select the database tables to update (typically all tables).
    • Dry run first to check how many fields will be changed without making actual modifications.
    • If the results look good, uncheck the "Dry Run" box and click "Run Search/Replace."
  3. Clear Cache
    • After completing the replacement, clear any cache (both on your website and server) to ensure changes reflect immediately.

Option 2: Using WP-CLI over SSH

If your package includes SSH, you can run WP-CLI commands directly on the server. SSH is available on every plan except Amethyst.

  1. Connect over SSH:
  2. Access Your WordPress Directory:
    • Navigate to your WordPress root directory (usually ~/public_html) by entering:
    cd ~/public_html
  3. Preview the changes first (dry run):
    • Run the command with --dry-run to see how many rows would change, without touching the database:
    wp search-replace 'staging.example.co.uk' 'example.co.uk' --all-tables-with-prefix --skip-columns=guid --dry-run
  4. Run the URL update:
    • If the preview looks right, run the same command again without --dry-run:
    wp search-replace 'staging.example.co.uk' 'example.co.uk' --all-tables-with-prefix --skip-columns=guid
    • --skip-columns=guid matters. WordPress is explicit that you must never change the GUID column - it is how feed readers recognise a post they have already shown, so rewriting it makes every post on your site appear brand new to every subscriber.
    • --all-tables-with-prefix covers tables belonging to this WordPress install. Avoid --all-tables, which rewrites every table in the database regardless of prefix - including any other application sharing it.
  5. Clear Cache:
    • After running the command, clear your website cache (if any) to ensure the changes take effect immediately.

How did we do?

Powered by HelpDocs (opens in a new tab)
© Krystal Hosting Ltd 2002–