To replace an old domain with a new domain in your website files, search the whole site folder for the bare old domain (for example olddomain.com, without http or www), review every match, then replace it with the new domain across all HTML, CSS, JavaScript, PHP, JSON, XML and config files. Searching for the bare domain catches every form in one pass: http and https URLs, www and non-www, email addresses and escaped URLs. Then set up 301 redirects from the old domain so existing links and rankings carry over.
Here is the full process, including the forms that simple searches miss and the places a file-level replace cannot reach.
Every form your old domain can take
Before replacing anything, know what you are looking for. A domain usually appears in several shapes:
| Form | Example | Caught by searching olddomain.com? |
|---|---|---|
| Full URL | https://www.olddomain.com/about/ | Yes |
| Non-www URL | http://olddomain.com/ | Yes |
| Protocol-relative | //olddomain.com/img/logo.png | Yes |
| Email address | info@olddomain.com | Yes |
| Subdomain | shop.olddomain.com | Yes, check you want it changed |
| Escaped in JSON | https:\/\/olddomain.com | Yes |
| URL-encoded | https%3A%2F%2Folddomain.com | Yes, the domain part itself is unchanged |
| Brand name in text | OldDomain or Old Domain Ltd | No, needs separate rules |
Because the domain part is the same in all URL forms, replacing olddomain.com with newdomain.com handles most of them at once and keeps the protocol, www and path exactly as they were.
Step 1: back up and count
- Back up the site folder or commit it to version control.
- Search for
olddomain.comacross all files and subfolders. Note the number of files and matches. - Scan the list for anything that should not change: a "formerly olddomain.com" note, a legal page, a subdomain that is staying, or a third-party service that still uses the old address.
Step 2: build precise rules
A single plain-text rule is often enough:
- Find:
olddomain.com - Replace:
newdomain.com
Make it more precise when needed:
- Avoid partial matches.
olddomain.comalso matches insidemyolddomain.comorolddomain.com.au. With regex, use(?<![\w-])olddomain\.com(?![\w-]|\.\w)to require the domain to stand on its own. Test it on your files, and review the preview. - Change the protocol at the same time. If the new domain is https-only, add a rule before the main one:
http://(www\.)?olddomain\.com→https://$1newdomain.com. - Brand names. Add separate rules for the old brand name in headings, titles and copy, longest form first.
If you are only moving from http to https on the same domain, our guide to changing http to https in all files is the better starting point.
Step 3: choose the file types
Domains hide in more places than HTML. Include:
.html .htm .php .aspand template files.css(background images, fonts, imports).js .json(API endpoints, config objects).xml(sitemaps, RSS feeds).txt(robots.txtsitemap line).htaccess,web.config,.ini,.conf,.envand other config files
Skip images, fonts, archives and PDFs. They are binary, and a text replace can damage them.
Step 4: replace with a preview
For a whole site, a preview and an undo are worth having. In Bulk Replace:
- Choose the site folder and tick include sub-folders. Filter to the extensions listed above. Binary files are skipped automatically.
- Add the rules: the https upgrade rule (if any), the main domain rule and any brand-name rules, in that order. Regex and plain-text rules can be mixed.
- Check the before/after diff for each file. Untick the matches you decided to keep in Step 1.
- Apply. Files are backed up first and one click restores the whole job.
If file or folder names include the old domain or brand, Bulk Replace can rename those in the same run, so links and file names stay in step. Each file's encoding is preserved, and the run is recorded in a timestamped report you can export as CSV.
Prefer a script? The PowerShell approach in our guide to replacing text in multiple files with PowerShell works too; use .Replace('olddomain.com','newdomain.com') for a literal match.
Example rule set for a domain move
Here is a typical set of rules for moving from olddomain.com to newdomain.com while also switching to https, in the order they should run:
| # | Find | Replace | Mode |
|---|---|---|---|
| 1 | http://(www\.)?olddomain\.com | https://$1newdomain.com | Regex |
| 2 | olddomain.com | newdomain.com | Plain text |
| 3 | Old Domain Ltd | New Domain Ltd | Plain text, match case |
| 4 | OldDomain | NewDomain | Plain text, whole word, match case |
Rule 1 upgrades the protocol on links that were still http. Rule 2 catches every other mention, including https links, emails and escaped JSON URLs. Rules 3 and 4 handle the brand, longest form first.
Step 5: what a file replace cannot reach
- Databases. WordPress and most CMSs store content and settings in a database. Use the CMS's own tools or a database search and replace utility that understands serialized data. A raw text replace on a SQL dump can break serialized PHP strings, because their stored lengths change.
- Hard-coded settings in hosting panels, CDN configuration, email routing and DNS.
- Third-party services: analytics, payment providers, OAuth callback URLs, newsletter tools and social profiles.
Step 6: redirects and checks
- Set up a permanent 301 redirect from every old URL to the matching new URL, keeping paths the same.
- Search the files again for
olddomain. The only matches left should be the ones you kept on purpose. - Open key pages, press F12 and check the Console and Network tabs for requests still going to the old domain.
- Update the canonical tags, sitemap and
robots.txtif the replace has not already done so, and submit the new sitemap to search engines. - In Google Search Console, verify the new domain and use the Change of Address tool.
Keep the old domain registered and redirecting for at least a year. Old links in emails, bookmarks and other sites will keep arriving long after the move.
Keep a record of the move
Save the list of rules and the files they changed. When a forgotten page turns up months later, or a colleague asks why an old address stopped working, the record answers the question in seconds.
Recap
- Search for the bare domain to catch every URL and email form.
- Guard against partial matches and decide which mentions to keep.
- Include CSS, JS, JSON, XML and config files, not just HTML.
- Preview, back up, apply, then fix the database and services separately.
- Redirect the old domain and verify nothing still points at it.
For the regex details behind patterns like $1, see regex replace with capture groups.
Frequently asked questions
Do I need to replace both www and non-www versions?
Search for the bare domain, such as olddomain.com, so you catch www, non-www, http, https and email addresses in one pass, then review the matches.
What about a domain stored in a database?
File-based find and replace only changes files. Domains stored in a CMS database need the CMS's own tools or a database search and replace that handles serialized data.
Should I set up redirects as well?
Yes. Replacing links fixes your own pages, but 301 redirects from the old domain keep search rankings and old bookmarks working.
