How to change the domain in WordPress. A complete technical guide
Changing a WordPress domain step by step: wp-config.php, WP-CLI search-replace, SQL queries, 301 redirects, the SSL certificate and the most common pitfalls.

Changing the domain in WordPress is an operation that looks simple on paper, but in practice it can knock the site offline for hours. The old domain is hard-coded in dozens of places: in the database, in configuration files, in serialised PHP arrays and in media paths. Below you will find the complete procedure I use for client domain migrations.
Prerequisites
Before you start, make sure you have:
- SSH access to the server (or at least phpMyAdmin)
- WP-CLI installed on the server (strongly recommended)
- A full backup of files and database
- Access to the DNS panel of the new domain
- Access to the hosting panel (to add the domain)
The backup is mandatory. Not "I think I have one somewhere on the host", but a fresh, verified backup of the database and files. Do not start without it.
Step 1. Point the new domain at the server
Log in to the hosting panel and add the new domain as an alias or addon domain pointing at the WordPress installation directory. Then configure the DNS records:
nowa-domena.pl. A 123.45.67.89
www.nowa-domena.pl. CNAME nowa-domena.pl.DNS propagation takes anywhere from a few minutes to 24 hours. Check progress:
dig nowa-domena.pl +short
# Should return your server's IPUntil DNS propagates, you can test the new domain by adding an entry to your local /etc/hosts:
123.45.67.89 nowa-domena.plStep 2. Force the new domain in wp-config.php
Open wp-config.php and add two constants before the require_once ABSPATH . 'wp-settings.php'; line:
define('WP_HOME', 'https://nowa-domena.pl');
define('WP_SITEURL', 'https://nowa-domena.pl');These constants override the siteurl and home values in the wp_options table. Once the file is saved WordPress will start responding under the new domain.
Important: Use https://, not http://. If SSL is not yet configured, use http:// temporarily and switch to https:// after step 4.
Step 3. Replace the domain in the database
This is the key step. The old domain sits in:
wp_options:siteurl,homeand dozens of plugin optionswp_posts:guid,post_content(links in content, image URLs)wp_postmeta: metadata, page builder data (Elementor, WPBakery)wp_comments: commenter URLs- Plugin-specific tables (WooCommerce, Yoast, etc.)
Recommended method: WP-CLI
# Dry run — preview the changes without applying them
wp search-replace 'https://stara-domena.pl' 'https://nowa-domena.pl' --dry-run --all-tables
# Run the replacement
wp search-replace 'https://stara-domena.pl' 'https://nowa-domena.pl' --all-tables
# Also handle the http and www variants
wp search-replace 'http://stara-domena.pl' 'https://nowa-domena.pl' --all-tables
wp search-replace 'http://www.stara-domena.pl' 'https://nowa-domena.pl' --all-tables
wp search-replace 'https://www.stara-domena.pl' 'https://nowa-domena.pl' --all-tablesThe --all-tables flag matters. Without it WP-CLI skips tables outside the standard wp_ prefix, including WooCommerce tables and tables added by other plugins.
Why WP-CLI rather than SQL? WP-CLI handles serialised PHP data correctly. WordPress stores many options as serialised arrays, e.g.:
a:2:{s:4:"name";s:22:"https://stara-domena.pl";s:3:"url";s:22:"https://stara-domena.pl";}Notice the s:22: — the string length. If you change the domain with a plain SQL REPLACE, the actual string length changes but the s:22: stays at the old value. WordPress will not be able to read such data. Widgets, theme options and plugin settings will break.
Fallback method: SQL (only for simple installations)
If you do not have WP-CLI and the site does not use page builders or complex plugins:
UPDATE wp_options
SET option_value = REPLACE(option_value, 'stara-domena.pl', 'nowa-domena.pl')
WHERE option_name IN ('siteurl', 'home');
UPDATE wp_posts
SET guid = REPLACE(guid, 'stara-domena.pl', 'nowa-domena.pl');
UPDATE wp_posts
SET post_content = REPLACE(post_content, 'stara-domena.pl', 'nowa-domena.pl');
UPDATE wp_postmeta
SET meta_value = REPLACE(meta_value, 'stara-domena.pl', 'nowa-domena.pl');
UPDATE wp_comments
SET comment_author_url = REPLACE(comment_author_url, 'stara-domena.pl', 'nowa-domena.pl');Caveat: These queries will break serialised data. For a simple blog this may not be a problem, but for WooCommerce shops or sites built with Elementor it is far better to use WP-CLI.
Step 4. The SSL certificate
The old certificate was issued for the old domain. Generate a new one:
# Let's Encrypt with Certbot (nginx)
certbot --nginx -d nowa-domena.pl -d www.nowa-domena.pl
# Let's Encrypt with Certbot (apache)
certbot --apache -d nowa-domena.pl -d www.nowa-domena.plIn hosting panels (cPanel, DirectAdmin) you generate the Let's Encrypt certificate with a single click in the SSL section.
After generating the certificate, switch the address in wp-config.php to https:// (if you used http:// earlier).
Step 5. The 301 redirect from the old domain
Keep the old domain alive with a 301 redirect for at least 6-12 months. That tells Google "the site has moved permanently" and passes the SEO value on to the new domain.
Apache (.htaccess)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?stara-domena\.pl$ [NC]
RewriteRule ^(.*)$ https://nowa-domena.pl/$1 [R=301,L]Nginx
server {
listen 80;
listen 443 ssl;
server_name stara-domena.pl www.stara-domena.pl;
return 301 https://nowa-domena.pl$request_uri;
}Do not delete the old domain. External links, user bookmarks and Google entries all still point at the old domain. The redirect makes sure those links keep working.
Step 6. Cleanup and verification
# Flush the WordPress cache
wp cache flush
# Clear transients
wp transient delete --all
# Flush rewrite rules
wp rewrite flushPost-migration checklist:
- The home page loads under the new domain
- The admin panel (
/wp-admin) works - Images render in the content
- Internal links go to the new domain
- The contact form sends mail
- WooCommerce: cart, checkout, payments (if applicable)
- The 301 redirect from the old domain works (
curl -I stara-domena.pl) - SSL works — green padlock, no mixed content
- Google Search Console: add the new domain, submit a change of address
- Google Analytics: update the website address
The most common problems
A blank page (White Screen of Death)
Almost always a typo in wp-config.php. Check the logs:
tail -f /var/log/php-error.log
# or
wp --debugA redirect loop (ERR_TOO_MANY_REDIRECTS)
A clash between forcing HTTPS in .htaccess, an SSL plugin (Really Simple SSL) and the constants in wp-config.php. The fix: disable the SSL plugin, remove the HTTPS rules from .htaccess, leave only the constants in wp-config.php.
Images do not load
The old domain is still in the src attributes of images inside wp_posts.post_content. Run wp search-replace again with the full URLs (including the scheme).
Mixed content (padlock with a warning)
Some assets (CSS, JS, images) load over http:// instead of https://. Run:
wp search-replace 'http://nowa-domena.pl' 'https://nowa-domena.pl' --all-tablesBroken widgets and theme options
You used SQL REPLACE instead of WP-CLI. The serialised data has the wrong string lengths. The only fix: restore the backup and repeat the procedure with WP-CLI.