By default, WordPress stores all image uploads in the wp-content/uploads folder of your website.
The images and media uploads can be further organized into nested sub-folders on the basis of month and year, this setting is available under Settings > Media within your WordPress administration area.
The default image upload folder works just fine, but there are situations when you may want to use a different folder for storing the images that are used in your blog posts and pages. Large websites, often use a sub-domain or a separate web server for storing and serving the images, while Amazon S3, MaxCDN are some good services that can be used to fetch images and other static files via a content delivery network.
The Problem Of Moving All Images From One Directory To Another Directory In WordPress
Let’s say you have stored images, videos and all static files at files.domain.com instead of the default image upload location wp-content/uploads/, which ships out of the box with WordPress. This was your initial setting 2 years back and during these two years, you have written 2500 blog posts and uploaded 2000 images (just an example).
Now your website has a decent amount of traffic and you want to speed up the loading time by fetching images via a cached folder or an external content delivery network such as Amazon S3 or MaxCDN.
There are two problems with images that are stored in a sub domain of your website or in any other folder that is outside the default image upload directory:
1. WordPress caching plugins such as W3 Total Cache and WP Super Cache can not cache files that are in a sub domain. These plugins will only cache internal files that are present within your WordPress media uploads folder ( the folder you have defined under Settings > Media).
2. You can surely download all the images from your image server and re-upload it to your external content delivery network. But the question is – how will you update all the image URL’s that is contained within all the old blog posts?
Move Images From One Directory To Another Without Breaking Your Blog Posts
Hence, moving all the images from one folder to another folder is just half the work done. You have to replace the older image URL’s with their newer counterparts, the following steps will tell you how to do that:
1. Use Filezilla or another FTP client to download the entire image upload directory on your computer.
2. Create the target folder and re-upload the entire image directory to its new target location on the same domain. It is advised to use the default image upload directory, if you are considering using caching plugins such as W3 Total cache.
For example: Lets say your older image folder is img.domain.com/images/ and you want to move all the images from this subdomain to wp-content/uploads/. Create a new folder at wp-content/uploads/old-images/ and upload the entire image directory here.
3. Be careful, you must preserve the file names of images and their corresponding folder structure. A single mistake in the name of any directory and the whole procedure will fail.
4. Take a back up of your WordPress database.
5. Install the Velvet Blues Update URL’s plugin. This script lets you perform a search and replace for URL’s in your WordPress database.
6. In the Old URL field, enter the base URL of your old media directory. In the new URL field, enter the base URL of your new media directory.
For example: http://img.domain.com/images/ will go in the old URL text field, while www.domain.com/wp-content/uploads/old-images/ will go in the new URL text field.
7. Click “Update URL’s Now” and its done. Load an old post and it should fetch the image from its new media directory.
If you find that the images are appearing broken and not loading at all, there is a high chance that you have messed up the image URL’s with a wrong address. In that case, restore the database backup and start the above procedure all over again.
The above technique can be used with external content delivery networks as well. All you have to do is get the newer base URL from the external CDN and replace it.
One word of caution: double check the folder structure in the target location. If its not a mirror copy of your older image server, updating the old image addresses to their newer counterparts won’t work.