Host your WordPress Images on Subdomain for Better Page Loading
Web browsers download a maximum of 4 files in parallel from a single domain, so if a page is having multiple number of images hosted in the same domain the page loading will be slower. So hosting the images in a sub-domain is one of the best way to improve the page loading of your website as the images and other resources could be downloaded in parallel. This will reduce the concurrent connections. So in this article I will explain how easily you can host images in a sub-domain for WordPress site.
WordPress does not support hosting of images in other domain but it supports hosting of images in a sub-domain. So lets see how to host the images for your WordPress posts in a sub-domain and we will also cover how to migrate the old images that are being hosted on the main domain to the sub-domain.
Step by Step Process to host WordPress images in a sub-domain
Create a Subdomain
First of all you need to create a subdomain of your main domain. The process of creating a subdomain depends on the hosting provider, but generally it is a very simple task.
Login to your hosting control panel and create your desired subdomain but I would recommend to create a subdomain with the name “img” (img.yourdomain.com) which points to your current upload directory which is generally “wp-content/uploads”, that is the document root directory of your subdomain should be “wp-content/uploads” (public_html/wp-content/uploads)
Change the default image upload location in wordpress
Login to your WordPress site with admin user. Go to Settings-Media for WordPress 3.0 on wards and Settings-Miscellaneous for wordpress older version. Make the changes as seen below.
“Store uploads in this folder”: The name of the folder where images will be stored, here it will be blank as still the images will be uploaded to the default directory.
“Full URL path to files”: The complete URL to the image location, in our case it would be : “http://img.yourdomain.com” then click on Save Changes.
Even after changing these configuration your files will be uploaded to the default directory wp-content/uploads, so you do not need to worry of taking extra backups and even if you migrate to a new hosting you just need to take the blog backup and create the subdomain.
Note: Till now you have completed the process of hosting your new images in the subdomain, how ever if you want to update the old image location follow the below steps.
Update old image location
Updating the old image location for the old post is a bit technical task and you need to be more careful while doing it. This step involves modification of your database content. You just need to run the SQL script.
If you have PHPMyAdmin then login to it and execute the below script for updating the old images in your post. Actually here we are going to replace the old image links in the post content with the new links. Please take a DB backup before executing the script to avoid any data losses due to any unwanted mistake.
UPDATE wp_posts SET post_content = REPLACE(post_content,'http://www.yourdomain.com/wp-content/uploads/','http://img.yourdomain.com/')
Note: Replace all yourdomain.com with your real domain name.
Now the process is completed successfully. Now the performance of your wordpress blog will increase by at least 10-15%. If you have any difficulty in implementing this please do let me know by providing your comments in “Jafaloo comment form not in FB comment form”.