How To Host Images of Your WordPress Website in a Subdomain

Advertisements

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.

  1. Login to the Hosting panel of your account and choose Subdomains and create a new subdomain eg. media.yoursitename.com/.

    creating subdomain for hosting images

  2. 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.

    creating seperate subdomain

    You could not only store images in the folder, but also your css and javascripts can be hosted in the subdomain, you only need make the desired changes to the themes files after that.

  3. As seen in the image above don’t select the check box “organize my uploads into month and year-based folders”.
  4. 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.
  5. Now all the images you upload from now on will be downloaded from the subdomain.
  6. 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.
  7. => Advanced Level Proceed with Caveat!
  8. 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.

    phpMyAdmin in wp

    running mysql query in phpMyAdmin for updating images upload  folder

  9. Now run the following mySQL query.
  10. UPDATE wp_posts SET post_content = REPLACE(post_content,'http://www.yourdomain.com/wp-content/uploads/','http://images.yourdomain.com/')
  11. Make sure you dont forget to make the necessary changes to the urls in the query before running it.
  12. 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.
  13. UPDATE wp_posts SET guid = REPLACE(guid,'http://www.yourdomain.com/wp-content/uploads/','http://images.yourdomain.com/')
  14. 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.
  15. RedirectMatch 301 ^/wp-content/uploads/(.*)$ http://images.yourdomain.com/$1
  16. Most probably this should be added before the # BEGIN WordPress line.
  17. That’s it, the images will be hosted in the new subdomain.

Amal Roy is the Founder-Editor of Computing Unleashed. A Technology Enthusiast, Windows Adept and a Proud Geek! | Connect with him on Google+