Most of webmasters are aware of the benefits of the loading speed of their website. The webpages with better page load time will rank better in major search engines like Google. Page load time is not only good for SEO purpose but also important for your website visitor, as they will likely spend more time on your blog or website.
One of the best ways of speeding up your website is to host the images of your site in another subdomain because of the parallel downloading ability of a browser. So, in this tutorial I will show you how to host your WordPress images in a subdomain and also redirect the old images that were hosted in the main domain.
Note : In WordPress 3.5, the media upload option is disable. See our post on How To Change Media Uploads Folder Path in WordPress 3.5.
How to host images in Subdomain
First create subdomain through your Webhosting control panel. For example : img.your-domain.com
The steps below assumes that you already created a subdomain in your webhost cPanel.
Change The Default Image Uploads Folder In WordPress
Log-in to your WordPress dashboard. Go to “Media” under “Settings” menu from left side of the dashboard.
Image may be NSFW.
Clik here to view.
Now change the default image upload folder which is wp-content/uploads and enter the sub-domain in the Full URL path to files with http in the beginning and no trailing slash (/) in the end. Do not select the Checkbox “organize my uploads into month and year-based folders” because it’s not good for SEO purpose.
Image may be NSFW.
Clik here to view.
Then Click on “Save Changes”. Now when you will upload any image to your post, the images will be uploaded in the subdomain.
Update Old Post Images Location
For changing the old post images, you need to copy or move all the folders containing the images under /public_html/wp-content/uploads/ to the folder of your subdomain. Then you need to execute a simple SQL query on your database to update the URL of the older posts images.
Open phpMyAdmin.
Image may be NSFW.
Clik here to view.
Select your WordPress database and click on the SQL tab.
Image may be NSFW.
Clik here to view.
Now run the following SQL query:
UPDATE wp_posts SET post_content = REPLACE(post_content,'http://www.your-domain.com/wp-content/uploads/','http://img.your-domain.com/')
Then click on “Go”. Make sure you don’t forget to make the necessary changes to the URLs in the query before running it. This query will update the URLs of images inside the posts to the new subdomain address.
To update the images URLs in the media library enter the following SQL command:
UPDATE wp_posts SET guid = REPLACE(guid,'http://www.your-domain.com/wp-content/uploads/','http://img.your-domain.com/')
Now, your old images files will point to the subdomain of your images.
301 Redirect Old Images Link to Subdomain
Other pages on the Web and Search engines might have indexed your images so it is wise to 301 redirect the old image URLs to the new images location. To do so, just add the following code in the .htaccess file in the WordPress root directory and save it. Replace img.your-domain.com with your actual URL.
NOTE: Backup your .htaccess file before proceeding.
RedirectMatch 301 ^/wp-content/uploads/(.*)$ http://img.your-domain.com/$1
That’s it, the images will be hosted in the new subdomain.