Fix Broken Image Links After a URL Change in WordPress

Fix Broken Image Links After a URL Change in WordPress

Blog URLs change for many reasons. You might move to a new domain entirely, or it may be an internal structure move. For example, you might decide to give up a subdomain at blog.example.com in favor of example.com/blog or vice versa. There are many guides on migrating your blog from one URL to another and specially between folders. But what about your images? If you don’t use an external CDN for them, the links to the old domain structure will remain embedded in your posts causing all of them to appear broken to whoever views the page. For a new blog this may not be a big deal if you can simply replace two or three URLs manually. But if yours is an established blog or if you have a huge number of images, it’s not feasible to go through all of them one at a time. What we need is a bulk method to replace all occurrences of a certain URL structure with another. Here’s how to fix broken image links in WordPress.

Searching and Replacing in the Database

Let’s say you use just migrated your blog to a new URL or folder. In my test example, I have two images in my post that have the “src” component set to “olddomain.com” instead of the correct one “lab.webhostingshed.com”.

old domain links

If you deleted the blog from the old location, these links to images are no longer valid and almost all of your posts will be littered with broken links like this:

images not displayed

This is just one post. There could be hundreds of others just like this one on your domain. As you can see, I can simply change the URLs manually by replacing olddomain with lab.webhostingshed.com, but I want a more efficient solution. For this purpose, I use the Search and Replace plugin that will make changes to the database directly. This way, it’s fast and efficient – but also a bit dangerous if you don’t know what you’re doing.

After downloading and installing the Search and Replace plug-in, you will find the settings under “Tools” on the left-hand side of the WordPress administration dashboard.

search replace settings

Over here, you can choose to either perform a simple search or a “search and replace” procedure. Just to make sure you’re not messing up anything important, I suggest going for a plain search at the beginning before you perform one that replaces everything. To do this, choose the “All – only search!” radio button, type in the search term that we want to change (in this case olddomain) and select the “Content” checkbox to search in the posts and click “Go”. This will perform a simple SQL search for all occurrences of olddomain thereby allowing you to see the various broken links.

Once you’ve ascertained that the search is proceeding as planned, select the other radio button called “All – search/replace!” and fill in the “Replace with” field with your new blog root – in this case lab.webhostingshed as shown below:

search replace

When the search and replace SQL query has been running on your database backend, you will be presented with the number of instances that have been replaced as shown below.

total replaced

In my test example, there are only a few such occurrences because I’ve created a dummy post. But in any large-scale blog, this should number in the hundreds or perhaps even the thousands.

That’s it! Once the search and replace function has run in the background, all of your image links should automatically have been replaced with your new blog address. As you can see in the screenshot below, my images have suddenly been corrected as well.

now it works!

Of course, ideally you should use an external CDN for hosting your images that doesn’t change even after a URL migration. But everyone has a different architectural set up and if you’re using internal links for your WordPress images, the Search and Replace plug-in is an ideal tool to fix your broken image links.

Leave a Reply

This Website is Hosted by

Powered by RamNode

Disclosure: We receive a compensation from some of the companies whose products or services are presented on our website.