Safely Update URLs After WordPress Migration with WP-CLI

by Fahim Mahmud Chisti

After a WordPress migration, the database still points at the old domain, and a raw SQL REPLACE() query to fix it breaks your site: widgets disappear, Customizer settings reset and page-builder layouts fall apart. WP-CLI search-replace updates every URL without that damage. This guide covers the safe sequence (backup, dry run, live run), multisite and plugin tables, Elementor’s escaped URLs, and how to check the result.

Quick answer: Back up the database, preview the change with wp search-replace --dry-run, then run it for real with --skip-columns=guid. Never use a raw SQL REPLACE(), because it breaks serialized data.

  1. Export the database: wp db export ~/pre-migration-backup.sql.
  2. Preview: wp search-replace 'https://old.com' 'https://new.com' --dry-run.
  3. Run it: the same command with --skip-columns=guid instead of --dry-run.
  4. Add --network on multisite, or --all-tables for plugin tables with other prefixes.
  5. Flush caches (and Elementor’s CSS), then check the site with curl -I.
In this guide
  1. Before You Start
  2. Why a Raw SQL REPLACE() Breaks WordPress
  3. Step 1: Back Up the Database
  4. Step 2: Preview the Change with –dry-run
  5. Step 3: Run search-replace for Real
  6. Step 4: Cover Multisite Networks and Plugin Tables
  7. Step 5: Limit the Run to Specific Tables on Large Sites
  8. Step 6: Fix Elementor URLs Stored as Escaped JSON
  9. Step 7: Flush Caches and Check the Site
  10. If Something Still Points to the Old Domain
  11. Frequently Asked Questions
  12. Next Steps
Close-up of a terminal screen showing WP-CLI search-replace command output and database tables

Before You Start

  • Access: SSH to the server that now holds the migrated site, with WP-CLI installed.
  • The exact old and new URLs: including http or https and www or not. Check the old one in the database with wp option get siteurl.
  • Time: 10–20 minutes; longer on very large databases.
  • Backup: Step 1 makes one. Do not skip it: a typo in the new URL is written into every table.

Why a Raw SQL REPLACE() Breaks WordPress

WordPress stores many settings, widget configurations and page-builder layouts as serialized PHP strings. A serialized string holds the text plus the exact character count of every string inside it.

Here is an ordinary serialized value in the wp_options table:

a:1:{s:8:"site_url";s:20:"http://staging.local";}

The s:20: before the URL tells PHP the string is exactly 20 characters long. If a MySQL REPLACE() query changes http://staging.local to https://example.com, the URL shrinks from 20 characters to 19, and the row becomes:

a:1:{s:8:"site_url";s:20:"https://example.com";}

The length still says 20, but the string has 19 characters. When WordPress reads this row with PHP’s unserialize() function, PHP hits an unexpected character, raises an error and returns false. WordPress treats the setting as missing and quietly falls back to defaults.

The WP-CLI search-replace command avoids this. It reads each row, unpacks the serialized data, swaps the string, recalculates the lengths and writes the row back.

Step 1: Back Up the Database

Never change database records without a copy on disk. If you mistype the domain or miss the protocol, you need a way back in seconds.

Connect over SSH, go to the WordPress root, and export the database to your home directory (never into the web root, where the dump could be downloaded):

cd /var/www/html
wp db export ~/pre-migration-backup.sql

WP-CLI reads the database details from wp-config.php, writes an uncompressed SQL file and confirms with a Success: line. If something goes wrong later, you can put this copy back; see how to restore a WordPress backup via SSH.

Check that the file exists and has a sensible size before you continue:

ls -lh ~/pre-migration-backup.sql

If the dump is only a few kilobytes for a site that has years of content, your database user may lack permissions, or wp-config.php points at an empty database. Do not run search-replace until you have a valid backup.

Step 2: Preview the Change with –dry-run

The --dry-run flag runs the whole search-replace, counts every match and writes nothing to the database.

Run a dry run from your old domain to the new one:

wp search-replace 'https://staging.devsite.net' 'https://example.com' --dry-run

When it finishes, WP-CLI prints a summary table of where it found matches, similar to this:

+-------------+--------------+--------------+------+
| Table       | Column       | Replacements | Type |
+-------------+--------------+--------------+------+
| wp_options  | option_value | 14           | PHP  |
| wp_posts    | post_content | 182          | SQL  |
| wp_posts    | guid         | 412          | SQL  |
| wp_postmeta | meta_value   | 68           | PHP  |
+-------------+--------------+--------------+------+
Success: 676 replacements to be made.

Look at the Type column. SQL means WP-CLI used a direct database query because the column has no serialized data. PHP means it unpacked and repacked serialized data, which is what keeps your settings intact. If the total is 0, the old URL in your command does not match the database exactly; check http versus https and www.

Step 3: Run search-replace for Real

When the dry run looks right, run the live command, but leave the guid column in wp_posts alone.

As the WordPress documentation on changing the site URL explains, post GUIDs are permanent IDs that feed readers use. If you change them, feed readers treat every post as new and notify subscribers again.

Skip that column with --skip-columns:

wp search-replace 'https://staging.devsite.net' 'https://example.com' --skip-columns=guid

WP-CLI prints the same table and ends with a Success: line saying how many replacements it made. Links, image sources, embeds and settings now use the new domain, while the GUIDs stay as they were.

If the migration also moved the site from HTTP to HTTPS, this command fixes most of the old http:// links in the database. Links hardcoded in theme files still need fixing by hand (see Next Steps).

Step 4: Cover Multisite Networks and Plugin Tables

On a multisite network, wp search-replace without extra flags only updates the main site’s tables. Tables for the other sites (like wp_2_posts or wp_3_options) stay unchanged.

To update every site in the network, add --network:

wp search-replace 'https://oldnetwork.com' 'https://newnetwork.com' --network --skip-columns=guid

After changing a network’s domain, also update DOMAIN_CURRENT_SITE in wp-config.php to match.

Some plugins create tables that do not use your WordPress table prefix, such as logging, audit or CRM tables. WP-CLI skips those by default.

To include every table in the database, add --all-tables:

wp search-replace 'https://oldnetwork.com' 'https://newnetwork.com' --all-tables --skip-columns=guid

--all-tables takes longer because it also scans tables that rarely contain URLs. On very large databases, stick to the default tables unless you know a plugin stores full URLs in its own tables.

Step 5: Limit the Run to Specific Tables on Large Sites

On large WooCommerce or high-traffic sites with hundreds of thousands of rows in wp_posts and wp_postmeta, scanning everything at once can time out or run out of PHP memory.

You can name the tables to update right after the two URLs. This example limits the run to options and posts:

wp search-replace 'https://oldurl.com' 'https://newurl.com' wp_options wp_posts --skip-columns=guid

To narrow it further, --include-columns updates only the columns you list, for example post content without titles or excerpts:

wp search-replace 'https://oldurl.com' 'https://newurl.com' wp_posts --include-columns=post_content

Limiting the run like this can cut the time a lot on large tables. For moving a large site from one server to another in the first place, see how to migrate a large WordPress site to Hostinger with SSH and WP-CLI.

Step 6: Fix Elementor URLs Stored as Escaped JSON

A common surprise after a full search-replace: Elementor pages are missing background images and icons, or still load files from the old domain, even though the database looks updated.

Elementor and some other page builders do two things a plain search-replace does not cover:

  1. They compile layout CSS into files inside wp-content/uploads/elementor/css/.
  2. They store design data as JSON in wp_postmeta, where every slash in a URL is escaped with a backslash.

Because of that escaping, the stored text does not literally contain https://oldurl.com, so WP-CLI finds nothing to replace in those rows, with or without --precise. Use Elementor’s own WP-CLI command, which understands how its data is encoded:

wp elementor replace-urls 'https://oldurl.com' 'https://newurl.com'

This does the same job as Elementor > Tools > Replace URL in the dashboard.

Then make Elementor rebuild its CSS files, also from the command line:

wp elementor flush-css

If you skip this, Elementor keeps serving old CSS files that point at the old URL, which shows up as mixed-content warnings or broken images.

Step 7: Flush Caches and Check the Site

Even with the database updated, WordPress may keep serving old values from an object cache such as Redis or Memcached until they are cleared.

Clear the object cache and rebuild the rewrite rules:

wp cache flush
wp rewrite flush --hard

The --hard flag also rewrites the rules in .htaccess on Apache-style servers. It has no effect on Nginx, where rewrite rules live in the server config.

Now check the homepage from your local terminal with curl:

curl -I https://example.com

You should see HTTP/2 200. If you get a 301 instead, look at the location header to see where it sends you.

If Something Still Points to the Old Domain

  • The site redirects back to the old domain. WP_HOME and WP_SITEURL in wp-config.php override the database. If they are set, change them to the new domain.
  • The dry run found 0 replacements. The old URL in your command does not match what is stored. Compare it with wp option get siteurl, including protocol and www.
  • Pages look right when logged in but not to visitors. A page cache or CDN still holds old HTML. Purge your cache plugin and CDN.
  • You replaced the wrong string. Put the backup from Step 1 back with wp db import ~/pre-migration-backup.sql and start again.

Frequently Asked Questions

Can I search-replace server paths as well as URLs?

Yes. wp search-replace works on any string, not just URLs. If the site’s absolute server path changed in the migration (for example from /home/user1/public_html to /var/www/example.com/html), run the same kind of command with the old and new paths:

wp search-replace '/home/user1/public_html' '/var/www/example.com/html' --skip-columns=guid

Do I need trailing slashes on my domain strings?

No, leave them off unless you are replacing a specific folder path. https://olddomain.com without a slash matches the bare site URL and every path under it (like https://olddomain.com/wp-content/uploads/photo.jpg). With a trailing slash, any stored value that omits the slash is missed.

What should I do if WP-CLI runs out of memory during search-replace?

Raise PHP’s memory limit for that one command. If you see Fatal error: Allowed memory size of X bytes exhausted, run WP-CLI through PHP with a higher limit:

php -d memory_limit=1024M $(which wp) search-replace 'https://old.com' 'https://new.com' --skip-columns=guid

This raises the limit to 1GB for this job only, without changing your server’s php.ini.

Why are my image thumbnails still pointing to the old URL?

Usually because a custom upload path is saved in the settings. If full-size images load but thumbnails point at the old server, check the upload_path and upload_url_path options:

wp option get upload_url_path

If it returns a full URL on your old server, set it to an empty string so WordPress builds upload URLs itself:

wp option update upload_url_path ''

Next Steps

  1. Create a test post and upload an image from the dashboard to confirm links and uploads use the new domain.
  2. Open a few pages with your browser’s DevTools console open. If you still see a broken padlock or “Mixed Content” warnings, follow the guide to fix WordPress mixed content warnings and force HTTPS.
  3. Once everything works, delete ~/pre-migration-backup.sql or move it somewhere safe off the server.
all_in_one_marketing_tool