WP-CLI Search-Replace to Change Domain or URLs Safely
The search-replace command handles serialized data correctly. Always run a dry run first.
Reference guide. This guide is written from established practice and the official documentation of the tools involved. We have not yet tested every step on a live demo site. Back up your site before you change anything.
Why use it
Plain text replace breaks serialized data. WP-CLI understands it.
Steps
- Back up the database. See WordPress backups.
- Dry run:
wp search-replace 'http://old.example' 'https://new.example' --dry-run. - Read the counts per table.
- Run it without
--dry-run. - Flush caches and save permalinks.
Tips
- Use exact strings including
https://. - Do not replace inside the GUID column on live sites unless you know why.
- Check images and menus afterwards.
Related guides
- WordPress: Clone a Site to a Subdomain for Testing
- Rollback Plan: How To Undo a Change in 5 Minutes
- Restore One File or One Database Table From a Backup Without Rolling Back Everything
- phpMyAdmin Import Fails: File Too Large or Script Timed Out
Did this solve it? If not, email hello@sitefixlab.org with the exact error text and we will look into it. Try the problem finder.