How to deal with a lot of missing images in Wordpress

Recently I had to start working on a new website. Usually I download the original one, setup it on my local machine and start developing. This site however had almost half a million of images. Of course I didn’t download them all because that would have taken forever and will eat up a lot of my local space (which is not that much to start with). But I said to myself “Not a biggie. Few missing images will not do much harm to my work.”. But it did …

See … because of the missing images and the default .htaccess rules WordPress is constantly trying to show its 404 Not Found page. And that page requires a bunch of system resources to fire up. Multiply these resources by the number of missing images and you are getting a high spike in CPU usage. So high that my Mac becomes inoperable. It becomes even worse if the 404 page also hasĀ missing images. Inception!

*insert image with Leo and his smushy face

I had this problem before. What I did is go to header.php of the theme and add a bit of code.

That will stop the page for fully loadingĀ and in most of the cases fix the high CPU issue. But that is just in some cases. Before opening header.php WordPress is doing a lot more in the background and still requires a significant amout of resources to get to the theme processing.

Then hot-linking came to help! I thought to myself … OK, I know the address of the missing images why don’t I just get them from the current site without downloading them. Brilliant! A few lines in the .htaccess

This is the original .htaccess that comes with WordPress and I added the code between the comments. What this code does is:
– Looks for a file with that path (if exists show the local file)
– Looks for a directory with that path (if exists show the local directory)
– (At this point the requested URL is missing). Redirects all requests for images (.jpg) to a local where the image exists (example.com/$1).

If hot linking is not an option for you (Because some servers forbid it) you can always redirect all the missing images to local image. Instead of:

you can use

The end result won’t be 1:1 with the original website but at least your local apache will not break and you will know that the image is missing.

P.S. I think I am writing this articles more for me and nobody reads them. If you do and you know a better or more elegant solution, please leave a comment.