Host images of WordPress site in Subdomain

There are a few advantages of hosting the images on your site to a subdomain. Your browser creates multiple connections to a server when opening a website. So, if the image file is 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 file, it’s wise to load them from a subdomain to speed up the website.

Note:- Before proceeding with ant thing, ensure that you have a backup of the WordPress database.

  • Log in to the Hosting panel of your account and choose Subdomains and create a new subdomain, e.g., media.yoursitename.com/.
creating subdomain for hosting images
  • 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. You could store images in the folder, and your CSS and javascript can be hosted in the subdomain; you only need to make the desired changes to the themes files after that.
creating seperate subdomain
  • 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, i.e. if you have organized the uploads into month and year-based folders before). So, please start using a new directory like public_html/imgs to upload your images.
  • All the images you upload from now on will be downloaded from the subdomain.
  • If you’ve got some WordPress expertise, 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/’,’http://images.yourdomain.com/’)
php my admin in the database
running mysql query in phpMyAdmin for updating images upload folder
  • Make sure you don’t 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 address of the new domain. Now we have the URLs 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 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/(.*)$  [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.
About the Author

Junaid is the senior editor at Computing Unleashed, he has damn interest and knowledge in computers, technology, and software. Outside of Computing Unleashed he has a professional digital marketing background where he has been working with agencies.

Junaid likes to explore tech and test new things, also loves to do exercises and keep himself fit.

Leave a Reply

Your email address will not be published. Required fields are marked *