How to change Wordpress URLs in the database
Wordpress has a nasty habit of redirecting you to what it thinks the correct domain name is, and this value is hidden in multiple obscure places in the database. If you've ever had to migrate a Wordpress site from one domain name to another you've probably encountered this problem.
Fortunately, there is a relatively painless way to reconfigure Wordpress to use the new domain name from inside its database.
First, connect to your Wordpress database like so:
$ mysql -h db.url.com -u username -p database_name
Enter your password when prompted (obviously). If you don't know the
URL or credentials for your database, check your
wp-config.php
file.
From the MySQL shell that you should now be connected to, run the
following queries, making sure to replace
http://oldname.com
and https://newname.com
with the actual old and new URLs of your site:
mysql> UPDATE wp_options SET option_value = replace(option_value, 'http://oldname.com', 'https://newname.com') WHERE option_name = 'home' OR option_name = 'siteurl';
mysql> UPDATE wp_posts SET guid = replace(guid, 'http://oldname.com', 'https://newname.com');
mysql> UPDATE wp_posts SET post_content = replace(post_content, 'http://oldname.com', 'https://newname.com');
mysql> UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://oldname.com', 'https://newname.com');
Not only will this fix those pesky redirects, it also fixes the metadata for all your blog posts.