How To Host Images on Subdomain for WordPress Blog

How To Host Images on Subdomain for WordPress Blog – Page load time is an important metric for Google search ranking.

The webpages with better loading time will rank better in the Google SERPS. In some previous articles, I’ve described about how to reduce the page load time of your WordPress blog. Another issue is still yet to discuss that effectively participate in your page performance.

By default the images of your site are hosted in the same domain. This slow downs your page speed. Because, almost all the browsers create maximum two-four connections when downloading a page from the web server.

So if your pages contain more than one images then it is wise to Host Images on Subdomain so that the browser can download the images in parallel with your main domain.

How to Host Images on Subdomain in WordPress

WordPress does not allow to host images on different domain. But fortunately, it supports and allows to host images on subdomain. This article is about how to host your website post images in a subdomain along with the old images which were hosted on the main domain.

Configure Your Domain to Host WordPress Images on Subdomain

In order to get your subdomain you’ve to log-in to your domain control panel.
Create Subdomain to Host Images on Subdomain
Point to Subdomains and create your subdomain, provided your domain resides under /public_html/.
Host WordPress Images on Subdomain
Note: The facility depends on your hosting provider. So if you are unable to create a subdomain then contact your hosting provider.

Serve WordPress Images From Subdomain

Once you created the subdomain from your hosting control panel, you can go forward to setup your WordPress blog to serve images from subdomain which you just created. You need to setup your site to serve both the new and old images from that. Here is how to do that.

Change Default Image Upload Folder to Subdomain in WordPress

This step involves setting up the WordPress to change the default uploading folder for the post images. log-in to your WordPress dashboard. Click on Settings > Media from left side of the dashboard.
Select Media To Change WordPress Image Path
Fill up the fields as follows.
Store uploads in this folder : images
Full URL path to files : http://images.yourdomain.com
Host WordPress Images on Subdomain Media Settings
Click on “Save Changes”. Now whenever you will upload any image to your post, the images will be uploaded to the subdomain: http://images.yourdomain.com.
Update: From the release of WP 3.5, the option for custom uploading path has been removed from the ‘Media settings’. But you can still choose the location of the ‘upload’ folder by following various methods. The easiest and best way to bring back the option under the WordPress ‘Media setting’ is using this plugin. This plugin brings the old option back. However, you can uninstall it after finishing the path setup as WordPress won’t remove those options until you make them as blank. So, if you are a user of WP 3.5 or higher version, just install the above plugin and get your job done. Once done, uninstall the plugin.

Update Old Post Images Location

You’ve just changed the upload location of your post images. So the images of the new posts will be uploaded into the subdomain. But what about the old post images? Changing the location of all the old images one by one is a stupid job. So, at first download all the folders containing the images under /public_html/wp-content/uploads/ from your server.
Update Older Image Path To Subdomain
Now, upload all those folders in the folder /public_html/images/. To update the new location of your images, you have to execute a simple SQL query on your database. You can do this from the phpMyAdmin in your domain control panel.
Update Image Path From phpMyAdmin
Click on phpMyAdmin > select your database from the left side > click on the “SQL” tab.
Sql Query To Transfer Images to Subdomain WordPress
Now put the following query in the box

UPDATE wp_posts SET post_content = REPLACE(post_content,'http://www.yourdomain.com/wp-content/uploads/','http://images.yourdomain.com/')

Sql Query To Update Old Image Paths WordPress
Click on “GO”. This query will replace "http://www.yourdomain.com/wp-content/uploads/"  with "http://images.yourdomain.com/" in all the image location URL. To update the images links in the media library run the following sql command:

UPDATE wp_posts SET guid = REPLACE(guid,'http://www.yourdomain.com/wp-content/uploads/','http://images.yourdomain.com/')

Redirect Old Images Link to Subdomain

You have completed all the task with your site and database. But Google has crawled and indexed your old images. So redirect the old image urls to the new image URLs to let Google know that those images are same. To do so add the following line in your .htaccess file.

RedirectMatch 301 ^/wp-content/uploads/(.*)$ http://images.yourdomain.com/$1

Reminder: Backup your .htaccess file before any change. That’s all to set your subdomain as post-images upload location.
Now this is time to tweak your website for better performance with great page load time. You can use various tools to optimize your website as well as you can view the page load times for your pages right from your Google Analytics account. If you have purchased MaxCDN account, learn how to Configure MaxCDN without Any Plugin in WordPress.
Did you serve images from subdomain in your WordPress blog? Do let us know. Also let us know if you follow different tricks to host images on subdomain in WordPress blog.