How to Change the WordPress URL in the Database: phpMyAdmin, SQL and WP-CLI

How to Change the WordPress URL in the Database: phpMyAdmin, SQL and WP-CLI

You need to change your WordPress URL when you move to a new domain, switch from http:// to https://, or migrate from a staging site. If you can still reach the admin, Settings → General is enough. But if the admin is locked or redirecting to the old address, you have to change it in the database.

There are two separate jobs here, and most guides only cover the first:

  1. The site address: two values in the wp_options table that tell WordPress where it lives.
  2. The links inside your content: every post, page, menu and widget that still points to the old URL.

This guide covers both. I recently did exactly this when this blog moved from an old domain and from http to https.

Before you start: back up the database

In phpMyAdmin, select your database → Export → Quick → SQL → Go. Keep that file until you’ve confirmed everything works. Every method below writes directly to your live data.

Method 1: phpMyAdmin (quickest when the admin is locked)

  1. Open phpMyAdmin from your hosting panel and select the WordPress database.
  2. Open the wp_options table. Your prefix may differ, for example wpab_options; check $table_prefix in wp-config.php.
  3. Find the rows where option_name is siteurl and home. They’re usually on the first page.
  4. Edit each option_value to the new URL, for example https://www.example.com. Don’t add a trailing slash.
  5. Save, then log in at the new address.

Method 2: SQL query

The same change in one go. Open the SQL tab and run (change the prefix and URL to yours):

UPDATE wp_options
SET option_value = 'https://www.example.com'
WHERE option_name IN ('siteurl', 'home');

Check it worked:

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

Method 3: wp-config.php (overrides the database)

If you can’t reach phpMyAdmin, add these two lines to wp-config.php above the line /* That's all, stop editing! */:

define('WP_HOME', 'https://www.example.com');
define('WP_SITEURL', 'https://www.example.com');

These override the database values, and the fields in Settings → General turn grey. It’s a good emergency fix. Once the site works, update the database values and remove the lines, so the setting lives in one place.

Step 2: Update links inside your content (the part most people miss)

Changing siteurl and home fixes the address, but your posts, images and menus still contain the old URL. You might try a plain SQL replace on wp_posts:

UPDATE wp_posts
SET post_content = REPLACE(post_content, 'http://old-domain.com', 'https://www.example.com');

That’s fine for post content. Don’t do this on wp_options or wp_postmeta. Many plugins and themes store settings as serialized PHP data, which records the length of every string. Change a URL’s length with a plain REPLACE and the stored length no longer matches. WordPress then can’t read the setting, and widgets or theme options quietly disappear.

The safe way is a tool that understands serialized data.

WP-CLI (recommended if your host provides SSH):

wp search-replace 'http://old-domain.com' 'https://www.example.com' --all-tables --dry-run

The --dry-run flag shows how many replacements would happen in each table without changing anything. When it looks right, run the same command without --dry-run.

No SSH? Use a search-and-replace plugin that handles serialized data (Better Search Replace is a common free one). Run it in dry-run mode first, just as with WP-CLI.

Leave the GUID column alone. wp_posts.guid is a permanent ID for each post, used by feed readers. WordPress’s own guidance is not to change it on a live site.

Step 3: Clean up

  1. Settings → Permalinks → Save Changes (without changing anything) to refresh rewrite rules.
  2. Clear any caching plugin, your host’s cache and your CDN cache.
  3. If you moved to https://, check for “mixed content”: pages that load images or scripts over http://. The browser console lists them.
  4. If you changed domain, add 301 redirects from the old domain to the new one (in .htaccess or your host’s panel), so old links and rankings carry over.
  5. In Google Search Console, add the new address and submit its sitemap.

Troubleshooting

  • Redirect loop after switching to https: your host may sit behind a proxy. Add if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { $_SERVER['HTTPS'] = 'on'; } to wp-config.php.
  • “Error establishing a database connection”: unrelated to the URL. Check the database name, user and password in wp-config.php.
  • Changes don’t seem to apply: WP_HOME or WP_SITEURL may already be defined in wp-config.php and overriding the database.

Frequently Asked Questions

Where is the WordPress site URL stored in the database?

In the wp_options table, in two rows: siteurl (where WordPress’s files live) and home (the address visitors use). They’re usually the same.

Is it safe to use SQL REPLACE to change URLs in WordPress?

On wp_posts.post_content, yes. On wp_options and wp_postmeta, no, because serialized data breaks when string lengths change. Use WP-CLI search-replace or a serialization-aware plugin there.

What’s the difference between siteurl and home?

siteurl is where the WordPress core files are installed; home is the public address of the site. They only differ when WordPress sits in a subfolder but the site is served from the domain root.

Do I need to change the GUID column?

No. GUIDs are permanent identifiers for feed readers, and WordPress recommends leaving them unchanged on a live site.