Introducing | Kaon — Creative Website Builder for WordPress & WooCommerce

Home How to replace URLs and Fix Broken Image URLs after Site Migration in WordPress | 2021

How to replace URLs and Fix Broken Image URLs after Site Migration in WordPress | 2021

In this article, you will learn how to replace URLs and how to fix broken image URLs either manually or with the help of a plugin.
How to replace URLs and Fix Broken Image URLs after Site Migration in WordPress | 2021
In this article, you will learn how to replace URLs and how to fix broken image URLs either manually or with the help of a plugin.

How to replace URLs and Fix Broken Image URLs after Site Migration in WordPress | 2021

Migration can be a painful process that requires persistent work and exceptional organization in order for the process to go as smoothly as possible. If you’ve already migrated your website, you might still be left with old URLs hanging and broken link images that don’t display properly. When migrating your WordPress website to a new URL you would need to update the old URL database including the MySQL database tables. 

If you run a small website with few URLs you can also update and replace images and URLs manually one by one, but when running a complex WordPress website it is not advisable to do this process one by one as you might risk forgetting some of the URLs, and it’s a time-consuming process. 

In this article, we will show you how to fix the broken image URLs and how you can replace URLs from the database. Since this is a comprehensive guide we will show you how you can replace images and URLs and replace them using MySQL and with the help of a plugin.

When to Replace images and URLs

The cases when you will need to update your site’s URLs and replace images and URLswill include scenarios:

  • When you move your website to a new domain name
  • After installing an SSL Certificate 
  • When you migrate your website to a new webserver 
  • Change WordPress directories on existing server 

You can easily change the WordPress address and site URL via the WordPress settings page, but this will not replace the URLs that you have previously added to your posts and pages. It also does not change the image URLs that you have uploaded to your website. We will show you how quickly and easily you can replace images and URLs in two different approaches. You can choose which route you take based on how confident you are in interfering with WordPress files and databases. 

How to change old URLs in the database

If you change the domain, location, or WordPress installation you will need to replace images and URLs for all the posts. Before we get into details we strongly suggest that you take a full backup of your website prior to making any changes on your website. If anything goes wrong you will still have the files saved to get back to your website. 

You can easily backup and restore your website using a plugin and it only takes you a single click. As mentioned, if you run a small website with only a few URLs you can easily change and replace the URLs manually one by one. But still, references in the old database will persist. 

That is why we recommend changing the old URLs directly in the database, although you must be careful as to how you are implementing the change. Two of the most subsequent issues that users face with old URLs are 

  • Old ULRs in widgets and menu – The old URL can also persist in widgets and menus not only on posts and pages 
  • Broken Image and Video Links – It happens that you have successfully replaced the site URL but one common issue is that the images are not functioning properly. If you do not update the image URLs they will not be displayed correctly. 

You can change all your site’s URLs directly from the database, which is where they are stored. You can find the URLs for custom menu items located in the meta_value field in the wp_postmeta table. To change the image URLs you will need to access inside the posts_content field in the wp_posts table. Please make sure you know exactly what URLs you are changing.

How to Change the Image URLs from the database

If you don’t change your domain name, which means you transfer your website from one server to another you can replace images and URLs by simply changing the website address on your wp-admin panel. If you are not using an external CDN for your images, then the links to the old domain structure will remain embedded in your posts and this causes them to appear broken whenever users view them. As you might know, WordPress runs on a MySQL database which allows you to execute raw queries as well as update old URLs. 

Here we will give you a step-by-step guide on how to fix the broken image URLs using MySQL so stick with us. The first step will be to open your cPanel account and locate the phpMyAdmin icon under the Database section. The phpMyAdmin will allow you to quickly update all the links on your website by updating the old ULR in the WordPress database.

Locate the phpMyAdmin section to find the WordPress databases

Note that various hosting providers present different displays of their cPanel, you need to locate the phpMyAdmin directorate. Now once you’ve gained access to the phpMyAdmin you will need to open the database by selecting the name from the left sidebar. If you don’t know the name of your database you need to go back to the database wizard to see.

How to replace Images and URLs after Site Migration in WordPress

Once you open the main database you will see all the corresponding databases from your website. The next step will be to open the wp_posts table.

Update and replace images and URLs using MySQL

Once you’ve done that click on the MySQL icon that’s shown above, this will open the MySQL editor.

Update and replace images and URLs using MySQL

There you should see the box to add the query. This is the structure of the SQL code

UPDATE wp_posts SET post_content=(REPLACE (post_content, '',''));

Now all you need to do is replace the old URLs and the way to do that by adding both the old and new domain. For example, if we want to change the old domain to then the SQL code structure would be

UPDATE wp_posts SET post_content=(REPLACE (post_content, '',''));
Add the code to replace the old URL

This code translates into all of your post links that have been replaced with your new URL. After the query execution is completed all posts and images will be updated within the new site URL. This method is bulletproof to replace images and URLs on your website as SQL query takes care of everything. 

To change or update the links across your entire website via MySQL you will need to follow all the same steps as shown above. But this time the code implemented will be a little different

UPDATE wp_options SET option_value = replace(option_value, 'Existing URL', 'New URL') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET post_content = replace(post_content, 'Existing URL', 'New URL');

UPDATE wp_postmeta SET meta_value = replace(meta_value,'Existing URL','New URL');

UPDATE wp_usermeta SET meta_value = replace(meta_value, 'Existing URL','New URL');

UPDATE wp_links SET link_url = replace(link_url, 'Existing URL','New URL');

UPDATE wp_comments SET comment_content = replace(comment_content , 'Existing URL','New URL');

Don’t forget to change the properties of the existing URL and New URL to ones that true to your website.

How to update old URL using WP CLI search-replace

If for some reason the above method does not work for you then we suggest you check out the WP-CLI commands to help you replace images and URLs. WP-CLI, WordPress Command Line Interface will ease the process of managing your WordPress website. By using the WP-CLI search and replace command you will be able to search for a string of data that you specify and replace it with another. We will show you how you can replace an old URL with a new URL using the WP-CLI command search-replace. 

Here too we advise you to take a full backup of your website before starting to make changes in case of unexpected results. 

  1. The first step will be to connect to SSH, for those who don’t know SSH also known as Secure Shell or Secure Socket Shell is a cryptographic network protocol that is used to perform network services accurately in an unsecured network. Login SSH to your cPanel as the site owner of the website you wish to make the changes. 
  2. The next step would be to use the cd command to switch to the document root directory of your website. For example cd public_html/my_website. Not that you should type in the exact route of the document root directory of your website 
  3. And thirdly, run the following command 
    wp search-replace {old URL} {new URL} –dry-run

Make sure to replace the old URL and new URL using the actual URLs of your website. The dry-run command will allow you to see see the results and when necessary to verify the results prior to making changes in the database.

How to fix broken image links using a plugin

If you don’t want to get your hands dirty with code or you don’t feel confident making changes in the core files of your website ( I’d be scared too) then a WordPress plugin is your best friend. Plugins make things easier as they do all the hard work on the background and you will be able to tweak complex tasks by just pressing a few clicks. You can replace images and URLs in minutes. For the purpose of this article, we will be using the Better Search Replace plugin to helps us along the process. The Better Search Replace plugin is a capable and popular WordPress plugin to help you ease the migration process to a new domain or server. 

This plugin will allow you to replace the URL and fix the image URLs within all or only a few database tables that you can select. First off you will need to install the plugin and activate it on your WordPress website. Upon Activation, you will find the plugin under Tools > Better Search Replace.

Use a plugin to replace URLs and images after site migration in WordPress

Next on you will need to fill in the details. In the field Search for type down your URL, while on the other Replace with field make sure to type in your new URL. You will also be able to select the database tables you wish to perform the changes. To select multiple databases use CRTL Click for Windows and CMD Click for Mac. 

Lastly, make sure to check the “Run a Dry” option so that you can view the changes prior to publishing your website. Click on Run Search and Analyze and you will be ready to go. There are other plugins that you can use to perform this process including Search & Replace or Velvet Blues.

Final Thoughts

We’ve shared a comprehensive guide on how to replace URLs as well as replace images and URLs and offered three different approaches that you can use based on your WordPress knowledge and expertise to resolve issues of site migration. 

You can replace the URLs manually, although that takes a lot of unnecessary effort and workload and you will also be at risk of forgetting URLs. The best choice is to bulk replace old URLs with new ones using the methods explained above. 

If the manual approach seems a bit too technical for you we would suggest you use the help of a plugin as we showed earlier. Any of the methods you use will help you fix the broken image URLs that are not displaying your media content properly. This can have a negative impact on conversion, so make sure you patch the issue before you finally publish your website. 

By Snowy Smith

Director of product design, Freehand

Introducing Kaon WordPress Theme