There are a few advantages of hosting the images in your site to a sub domain. Your browser creates multiple connections to a server when opening a website. So, if the image file are uploaded / hosted in a separate domain / subdomain the downloads will be done in parallel and your website will load fast. So when your blog contains more than one image files then its wise to load them from a subdomain to speed up website.
Note:- Before proceeding with ant thing ensure that you have a backup of the wordpress database.
- Login to the Hosting panel of your account and choose Subdomains and create a new subdomain eg. media.yoursitename.com/.
- Now login to your wordpress dashboard and navigate to Settings > Miscellaneous (“Media” in WordPress 3.0) and enter the new location for the images upload folder.
- As seen in the image above don’t select the check box “organize my uploads into month and year-based folders”.
- Here I have used the default wordpress images folder. (It might create a problem if you have subdirectories in there ie if you have organized the uploads into month and year-based folders before). So, its better if you could start using a new directory like public_html/imgs to upload your images.
- Now all the images you upload from now on will be downloaded from the subdomain.
- If you’ve got some expertise in wordpress then you can continue with the rest of the steps. Else stopping here would do just fine.
- => Advanced Level Proceed with Caveat!
- If you want to update the images in your post and media library to the new address, so that the previous images also will be pushed to the subdomain, just run these queries from phpMyAdmin.
- Now run the following mySQL query.
UPDATE wp_posts SET post_content = REPLACE(post_content,'http://www.yourdomain.com/wp-content/uploads/'
- Make sure you dont forget to make the necessary changes to the urls in the query before running it.
- The url of images inside the posts will be updated by now with the new domains address. Now we have the url’s in the media library left. So run the following query too.
UPDATE wp_posts SET guid = REPLACE(guid,'http://www.yourdomain.com/wp-content/uploads/','http://images.yourdomain.com/')
- Now we need to update the search engines too with the new address for the images and also we need to avoid duplicate content as google would now see two urls for the same image. So open up your .htaccess file in the wordpress root directory and add the following code to it.
RedirectMatch 301 ^/wp-content/uploads/(.*)$ http://images.yourdomain.com/$1
- Most probably this should be added before the # BEGIN WordPress line.
- That’s it, the images will be hosted in the new subdomain.