Step by step how to serve images from subdomain
If you have read my earlier posts like Plugins to speed up your wordpress website 2014 you must be knowing that i specially emphasize on the page speed. Why i do this is because of Google, they love fast loading websites. If you know about checking the speed of website you must be knowing about how to serve images from a subdomain.
In my other post explained how to use lazy load image to speed up your website, this post was about how to reduce your page load time. Having a good website speed is very essential for SEO. There are many ways you can increase your website speed, one of the nice way to achieve this by serving images from subdomain. If you research a little and look at some of the top websites you’ll find out that they serve static content such as images from sub-domain.
How to serve images from sub-domain
Serving images from a sub-domain is very easy, even if you have no knowledge about coding. Just follow step by step and you are good to go.
1 Creating a Subdomain
First you have to login to the cpanel of your website, there in the Domain section you have to select the option Sub-domain.
when you click this option you see a window in which you see a tab in front of subdomain. You can name your subdomain in front of your domain name such as img, a simple i or any thing you desire. For WordPress users like me document root to public_html/wp-content/uploads this is where your images are stored. If you are using other CMS then WordPress, then you have to link document root to the directory where you images are stored.
Configure WordPress
Now you have to head to your wordpress website and login to your dashboard. then go to settings>media and in Full URL path to files you have to enter http://img.example.com (where you should replace example.com with your website name).
This will help you to store your images in img.example.com directly from WordPress when you add an image.
Making changes in phpmyadmin
Now again you have to go to your cpanel then under databases you’ll find an icon names phpmyadmin. This step will redirect your older images to the new subdomain.
After entering the phpmyadmin panel you have to select your active database and then click on the SQL tab and enter the code for redirecting your images
UPDATE wp_posts SET post_content = REPLACE(post_content,’http://www.example.com/wp-content/uploads/’,’http://img.example.com/’)
Paste this code in SQL query box and hit go to run this and entries will be added to your database.
Adding codes in .htaccess file
Now for the final part we have to redirect your images which you have already uploaded to the new address. When you make a post and it gets indexed by google and other search engines then the images are also indexed, by placing this code in .htaccess file will help them find the new address. In case you don’t have a .htaccess file you should create one, this file is read by search engines as where is redirection and paths of the files.
RedirectMatch 301 ^/wp-content/uploads/(.*)$ http://img.example.com/$1
Don’t forget to replace example.com with your own website name.
Further read: