Replace old to new url in the WordPress database

If you are new in WordPress development and working on a client project then one thing you have to take note is that whenever you deploy your WordPress project to client-server you have to replace your site URL from local to live and the same thing will apply, If you migrate from one to another domain. If you forgot to change URL then it will redirect to the old URL and that needs to take care of it.

Another way you can avoid this problem by creating a virtual host in your local server with a live domain.

replace old to new url in the wordpress database

So how to replace old to new URL in the WordPress database? Well, it’s very easy. You just have to replace your old URL with the new URL so follow the below steps.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Step 1:

If you have already uploaded the database to the server, then go to your phpMyAdmin and open your database.

Step 2:

Copy the below code and replace your old URL with http://www.oldurl.in and do the same for the new URL.

UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl.in', 'https://www.mananpatel.in') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl.in','https://www.mananpatel.in');

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl.in', 'https://www.mananpatel.in');

UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl.in','https://www.mananpatel.in');

After replacing your old & new URL, go to the SQL section in your phpMyAdmin and past your SQL queries and hit the Go button.

After hitting the Go button, SQL queries will replace all instances of old URLs to new URLs. For example, If your current WordPress address is http://www.oldurl.in, it will be changed to https://www.mananpatel.in.

IMPORTANT! Sometimes table prefix can be different than wp_. If you get error messages saying that tables wp_optionswp_postswp_posts and wp_postmeta cannot be found, check table prefix and update SQL query code. You can see table prefix on the left side of phpMyAdmin, under your WordPress database:

Step 3:

If the query was executed successfully, then you will see a green success message with the number of records is affected.

Keep visiting for new stuff and give your feedback.

Happy Coding 😉

Author: Manan Patel

Freelancer + Passionate web developer focused on backend and frontend development.

Leave a Reply