WordPress

Search and replace URLs in the WordPress database safely

Replace old URLs or text across a WordPress database without breaking serialised data, using WP-CLI or a plugin, with a dry run and backup first.

IntermediatecPanel hosting3 min read

After a migration, a domain change or a switch to HTTPS, the old address remains inside posts, menus, widget settings and page builder data. Replacing it everywhere is easy, but only with a tool that understands WordPress's serialised data.

Why not plain SQL?

WordPress stores many settings as serialised PHP strings that include each string's length, for example s:22:"https://old-domain.com". A simple SQL REPLACE() changes the text but not the length, so PHP can no longer read the value. Widgets disappear, theme options reset and page builders lose layouts. WP-CLI and dedicated plugins unserialise, replace and re-serialise each value.

Before you start

  • Back up the database: cPanel → Files → Backup → Download a MySQL Database Backup, or wp db export.
  • Write down the exact old and new strings, including https:// and with or without www. Do not include a trailing slash.

Method 1: WP-CLI

  1. Open cPanel → Advanced → Terminal and go to the site folder with cd ~/public_html.
  2. Do a dry run to see how many replacements will be made:
wp search-replace 'https://old-domain.com' 'https://new-domain.com' --all-tables --precise --dry-run
  1. If the numbers look sensible, run it again without --dry-run.
  2. Flush caches: wp cache flush and purge LiteSpeed Cache.

Useful options: --skip-columns=guid leaves post GUIDs alone (recommended on established sites, since feed readers use them), and --report-changed-only keeps the output short.

Method 2: Better Search Replace plugin

  1. Install and activate Better Search Replace from Plugins → Add New.
  2. Open Tools → Better Search Replace.
  3. Enter the old string in Search for and the new one in Replace with.
  4. Select all tables (hold Ctrl or Cmd), leave Run as dry run? ticked and click Run Search/Replace.
  5. Review the report, untick the dry run option and run again.
  6. Deactivate and delete the plugin when finished.

Common replacements

SituationSearchReplace
HTTP to HTTPShttp://example.comhttps://example.com
Add wwwhttps://example.comhttps://www.example.com
New domainhttps://old.comhttps://new.com
Staging to livehttps://staging.example.comhttps://example.com
Server path change/home/olduser/public_html/home/newuser/public_html

Run the path replacement after migrations between cPanel accounts with different usernames; some plugins store absolute paths.

Page builders and CSS files

Some builders cache generated CSS in files. After replacing, regenerate them, for example Elementor → Tools → Regenerate CSS & Data, and purge all caches.

Troubleshooting

  • Zero replacements: check for typos and whether the old URL used www or http.
  • Some images still use the old URL: they are hard-coded in theme files or in CSS; search the files too.
  • Site broken after a replace: restore the database backup and repeat with the correct strings.
  • The plugin times out on a large database: lower the Max Page Size setting, or use WP-CLI, which is not limited by web timeouts.

Replacing in a multisite network

In a Multisite install, tables for each site have their own prefix such as wp_2_posts. With WP-CLI add --network to process every site, or --url=site.example.com to target one. Also update the domain in the wp_blogs and wp_site tables and the DOMAIN_CURRENT_SITE constant in wp-config.php, which search and replace does not cover.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.