When a WordPress site moves to a new domain, switches from http:// to https://, or comes back from a staging copy, the old address stays buried in thousands of places in the database: links in posts, image paths, widget settings, page-builder layouts. This guide shows how to search and replace your domain in a WordPress database safely, using WP-CLI over SSH or a plugin, and why you should never do it with a plain SQL query.
Why a normal find-and-replace breaks WordPress
Many plugins and themes save settings as serialized data -- text that records the length of every value. For example:
s:22:"http://old-example.com";
The 22 is the number of characters in the address. If you change the address to https://www.new-example.com with a plain SQL REPLACE() or a text editor, the length no longer matches. WordPress then can't read that setting at all, and widgets vanish, theme options reset and page builders show blank pages.
WP-CLI's search-replace command and dedicated plugins understand serialized data: they unpack each value, replace the text and write it back with the correct length. That's why they're the only safe way to do this.
Before you start
- Back up the database. A search and replace touches almost every table. You'll make a copy in the first step below; your automatic JetBackup 5 backups are a second safety net.
- Know the exact old and new addresses. Look at Settings > General in WordPress, or the
siteurlandhomerows in thewp_optionstable in phpMyAdmin. Note whether the old one usedwwwandhttporhttps. - Make sure the new domain works. It should point to your hosting account and have an SSL certificate (AutoSSL issues one automatically once DNS points here). See the related guides.
- Moving from another host? WP Toolkit's import tool updates the address for you in most cases -- you may not need this guide at all.
Method 1: WP-CLI (recommended)
WP-CLI is installed on our servers as the wp command. It's fast, handles serialized data, and lets you preview every change before making it.
Step 1: Open a terminal in your WordPress folder
- In cPanel, go to Advanced > Terminal (or connect with SSH on port 22).
- Go to the folder WordPress is installed in:
cd ~/public_html - Check WP-CLI can see the site:
It should print the current (old) address, for examplewp option get siteurlhttp://old-example.com.
Step 2: Export a copy of the database
wp db export ~/before-search-replace.sql
This saves the copy in your home directory, outside the public website folder.
Step 3: Do a dry run
wp search-replace 'http://old-example.com' 'https://new-example.com' \
--all-tables-with-prefix --skip-columns=guid --dry-run --report-changed-only
Nothing is changed yet. You'll see a table listing each table and column, and how many replacements would be made -- for example wp_posts | post_content | 212 -- then a line like Success: 348 replacements to be made. If the count is zero, check the old address for typos (a missing www or s is common).
Step 4: Run it for real
wp search-replace 'http://old-example.com' 'https://new-example.com' \
--all-tables-with-prefix --skip-columns=guid --report-changed-only
You'll see Success: Made 348 replacements.
Step 5: Catch the variations
Sites often contain the old address in more than one form. Run the dry run again for each variation you find, for example:
wp search-replace 'http://www.old-example.com' 'https://new-example.com' --all-tables-with-prefix --skip-columns=guid --dry-run
wp search-replace '//old-example.com' '//new-example.com' --all-tables-with-prefix --skip-columns=guid --dry-run
Some page builders store addresses with escaped slashes, like http:\/\/old-example.com. If the builder's pages still point to the old site, search for that form too (in single quotes, exactly as it appears).
Step 6: Clear caches
wp cache flush
wp litespeed-purge all
The second command purges LiteSpeed Cache if that plugin is active. Then open the site in a private browser window and click around.
Step 7: Check nothing was missed
Search the whole database for any remaining mention of the old domain:
wp db search 'old-example.com' --all-tables
Each match is listed with its table and column. Matches in the guid column are expected (we skipped it on purpose). Old addresses in comments or log tables are usually harmless. Anything in wp_posts, wp_postmeta or wp_options is worth a closer look -- run another dry run with that exact form of the address. Also look outside the database: theme files, .htaccess and hard-coded links in custom code aren't touched by search-replace. In Advanced > Terminal, grep -rl 'old-example.com' ~/public_html --include='*.php' --include='.htaccess' lists any files that still contain it.
What the options mean
| Option | Why we use it |
|---|---|
--dry-run | Shows what would change, changes nothing. |
--all-tables-with-prefix | Includes plugin tables that use your WordPress table prefix, not just the core ones. (--all-tables goes further and includes every table in the database, even ones from other software.) |
--skip-columns=guid | Leaves post GUIDs alone. They're permanent IDs used by feed readers, not links, and WordPress recommends not changing them. |
--report-changed-only | Hides the tables with nothing to change, so the output is easier to read. |
--precise | Slower but more thorough; try it if some serialized data isn't being updated. |
For a multisite network, add --network to cover every site, and check the WP-CLI documentation first -- multisite domain changes have extra steps.
Method 2: A plugin (no command line)
If you'd rather stay in the WordPress dashboard, a search-and-replace plugin such as Better Search Replace does the same job and also handles serialized data.
- Export your database first: in cPanel, Databases > phpMyAdmin, click the database, Export, Export.
- In WordPress, go to Plugins > Add New, search for the plugin, install and activate it.
- Open its page (for Better Search Replace, Tools > Better Search Replace).
- Enter the old address in Search for and the new one in Replace with.
- Select all tables in the list (click the first, then Shift-click the last).
- Leave Replace GUIDs unticked and keep Run as dry run? ticked. Click Run Search/Replace.
- Check the results. If the numbers look right, untick Run as dry run? and run it again.
- Purge LiteSpeed Cache (LiteSpeed Cache > Toolbox > Purge All) and test the site.
- Deactivate and delete the plugin when you're done. It's a powerful tool to leave lying around.
On very large databases, a plugin can time out part way. WP-CLI doesn't have that problem.
If you can't log in to WordPress at all
If the site redirects to the old domain and you can't reach the dashboard, fix the two main address settings first, then run the full search and replace:
wp option update home 'https://new-example.com'
wp option update siteurl 'https://new-example.com'
Or, without SSH, edit the siteurl and home rows in the wp_options table in phpMyAdmin. These two values are plain text, not serialized, so editing them directly is safe.
Troubleshooting
"Error: This does not seem to be a WordPress installation."
You're not in the WordPress folder. Run cd ~/public_html (or the folder that contains wp-config.php) and try again, or add --path=/home/username/public_html to the command.
WP-CLI shows PHP warnings or a plugin error
A plugin or theme is throwing errors when WP-CLI loads. Add --skip-plugins --skip-themes to the command -- search-replace doesn't need them loaded.
Widgets or theme settings disappeared afterwards
Serialized data was damaged, usually by a plain SQL replace or a text editor rather than WP-CLI or a plugin. Import the copy you exported before you started, then redo the change with WP-CLI.
"Not secure" warnings after switching to https
Some content still loads over http://. Run a dry run for http://new-example.com to https://new-example.com, and see our mixed content guide.
Too many redirects after changing the domain
Often a redirect rule in .htaccess, a plugin, or a CDN is fighting the new address. See the redirect loop guide in the related links.
Images still load from the old domain
Check for hard-coded addresses in theme files or a CDN setting in your caching plugin, and clear every cache (plugin, LiteSpeed and browser).
Common questions
Should I include "http://" or just the domain?
Include the scheme (http:// or https://) so you don't accidentally change email addresses or unrelated text that contains the domain. Then run separate passes for other variations you find.
Will this change my email addresses?
Not if you search for the full URL including http:// or https://. If you search for the bare domain, addresses like info@old-example.com will change too.
Can I undo it?
Import the copy you made in Step 2 (wp db import ~/before-search-replace.sql), or restore the database from JetBackup 5.
Related guides
- How to Move a WordPress Site from Another Host (WP Toolkit Import)
- How to Create a WordPress Staging Site and Push It Live
- How to Fix "Not Secure" Warnings and Mixed Content Errors
- ERR_TOO_MANY_REDIRECTS: How to Fix a Redirect Loop on Your Website
- How to Use SSH, SSH Keys and Terminal in cPanel
- How to Clear (Purge) the LiteSpeed Cache in WordPress
Still stuck? Open a support ticket and the Instant Access Internet Services team will help.
