Introduction

Changing the WordPress Site URL is a common task when migrating a website to a new domain, moving from HTTP to HTTPS, restoring a backup, or deploying a site to a new server. While WordPress provides options to update the site address from the admin dashboard, there are situations where accessing the dashboard is not possible due to configuration issues, domain changes, or migration-related problems.

In such cases, updating the Site URL directly from the MySQL or MariaDB database is a reliable and efficient solution. WordPress stores website URLs in multiple database tables, including site settings, posts, pages, media attachments, and custom fields. To ensure the website functions correctly after a domain change, all references to the old URL must be updated.

This guide demonstrates how to safely modify the WordPress Site URL using SQL queries, verify the changes, and avoid common issues that may arise during the migration process. Whether you are changing domains, enabling HTTPS, or moving a WordPress installation to a new environment, these steps will help ensure a smooth transition.

Prerequisites

Before proceeding, ensure that:

  • WordPress is already installed.
  • You have access to the MySQL or MariaDB server.
  • You know the database name used by WordPress.
  • You have taken a complete database backup.

This guide works regardless of the web server being used (Nginx, Apache, LiteSpeed, etc.) and is applicable to both MySQL and MariaDB databases.

Scenario

Assume your current WordPress website URL is:

http://domainA.com

You want to change it to:

http://domainB.com

Step 1: Backup the Database

Always create a backup before making any database modifications.

mysqldump -u root -p wordpress_db > wordpress_backup.sql

Replace wordpress_db with your actual database name.

Step 2: Login to MySQL or MariaDB

Connect to the database server:

mysql -u root -p

Select the WordPress database:

USE wordpress_db;

Step 3: Update the WordPress Site URL

The WordPress site URL is stored in the wp_options table.

Execute the following query:

UPDATE wp_options
SET option_value = REPLACE(
    option_value,
    'http://domainA.com',
    'http://domainB.com'
)
WHERE option_name IN ('home', 'siteurl');

This updates both:

  • home – The website homepage URL
  • siteurl – The WordPress installation URL

Step 4: Update URLs in Posts and Pages

WordPress stores links and media references inside post content. Update them using:

UPDATE wp_posts
SET post_content = REPLACE(
    post_content,
    'http://domainA.com',
    'http://domainB.com'
);

Step 5: Update GUID Values

The GUID field contains permanent identifiers for posts and attachments.

UPDATE wp_posts
SET guid = REPLACE(
    guid,
    'http://domainA.com',
    'http://domainB.com'
);

Step 6: Update URLs in Custom Fields

Many themes and plugins store URLs in the wp_postmeta table.

UPDATE wp_postmeta
SET meta_value = REPLACE(
    meta_value,
    'http://domainA.com',
    'http://domainB.com'
);

Step 7: Update HTTP to HTTPS (Optional)

If you are migrating from HTTP to HTTPS, update the URLs accordingly.

Update Site URL

UPDATE wp_options
SET option_value = REPLACE(
    option_value,
    'http://domainA.com',
    'https://domainB.com'
)
WHERE option_name IN ('home', 'siteurl');

Update Post Content

UPDATE wp_posts
SET post_content = REPLACE(
    post_content,
    'http://domainA.com',
    'https://domainB.com'
);

Update GUID Values

UPDATE wp_posts
SET guid = REPLACE(
    guid,
    'http://domainA.com',
    'https://domainB.com'
);

Update Custom Fields

UPDATE wp_postmeta
SET meta_value = REPLACE(
    meta_value,
    'http://domainA.com',
    'https://domainB.com'
);

Step 8: Verify the Changes

Check whether the URLs have been updated successfully.

SELECT option_name, option_value
FROM wp_options
WHERE option_name IN ('home', 'siteurl');

Example output:

home      https://domainB.com
siteurl   https://domainB.com

Step 9: Clear Cache and Restart Services

After updating the database:

Clear WordPress Cache

If you use caching plugins such as:

  • WP Rocket
  • LiteSpeed Cache
  • W3 Total Cache
  • WP Super Cache

Clear all cached data.

Clear Redis Cache

redis-cli FLUSHALL

Restart PHP-FPM

systemctl restart php-fpm

Or:

systemctl restart php8.2-fpm

Recommended Method: WP-CLI

Direct database updates work well for simple websites. However, modern WordPress installations often contain serialized data stored by themes and plugins.

Updating serialized data with SQL can corrupt values and break the website.

A safer approach is to use WP-CLI:

wp search-replace 'https://domainA.com' 'https://domainB.com' --all-tables

Benefits of WP-CLI:

  • Handles serialized data correctly
  • Updates all WordPress tables
  • Reduces the risk of data corruption
  • Faster and more reliable for large websites

Troubleshooting

Site Redirects Back to the Old Domain

  • Clear browser cache.
  • Clear WordPress cache plugins.
  • Verify home and siteurl values in wp_options.
  • Check web server redirect rules.

Images Are Not Loading

Run a search and replace operation on the database to update image URLs stored in posts and metadata.

Mixed Content Errors After HTTPS Migration

Ensure all HTTP references have been updated to HTTPS.

You can identify mixed content issues using browser developer tools.

Conclusion

Changing a WordPress site’s URL directly from MySQL or MariaDB is a quick and effective solution when migrating domains, moving to HTTPS, or restoring backups. While SQL queries are suitable for basic URL changes, WP-CLI is the preferred method for production environments because it safely handles serialized data and plugin-specific configurations.

Always back up your database before making changes and verify the website thoroughly after updating the URLs.

Leave a Reply