Back to my Blog

Updating URL’s in the database after WordPress migration


Last modified: 17th February 2023

I’m putting this here as it’s a code snippet I use all the time and if anything, it makes it easier for me to find.

Once you’ve migrated a WordPress site from one hosting to another (or set up a site locally) you will likely want to update all the URL’s throughout your website so they point to the correct place.

(I use phpmyadmin so I will talk about it in that context.)

Once you’ve imported your database into phpmyadmin, you’ll want to navigate to the SQL tab (see image below)

SQL tab within phpmyadmin

Then you’ll want to paste in the following code:

UPDATE wp_options SET option_value = replace(option_value, 'https://oldurl.com', 'https://newurl.com') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'https://oldurl.com','https://newurl.com');

UPDATE wp_posts SET post_content = replace(post_content, 'https://oldurl.com', 'https://newurl.com');

UPDATE wp_postmeta SET meta_value = replace(meta_value,'https://oldurl.com','https://newurl.com');

You’ll need to replace the https://www.oldurl.com and https://newurl.com with your own URLs (don’t include trailing slashes).

Then press ‘Go’ and it should let you know how many rows it has replaced the URL in.

Leave a Reply

Your email address will not be published. Required fields are marked *


Previous Post:
Next Post: