Host Images on a Subdomain in WordPress 3.6
Today I’m going to tell you how to host images in a sub-domain your website running on wordpress. Not everyone knows why one should do this. So first let me tell you why a webmaster should host images of his wordpress website in a subdomain.
When you visit a website, the browser downloads a maximum of 4 files from the website in parallel. Now if your webpage has many images, the browser will take time in downloading the contents of the website as it has to download both files and images from a single domain. If you make a separate domain (here as subdomain) then you can make the browser download files as 8 files in parallel. That means you can theoretically make the loading time of the website decrease to half. The concept here is to create a content delivery network (cdn) for you wordpress website.
There is also another way of making use of high speed cdn and that too for free for wordpress. We will cover it next time.
1. Create a subdomain:
Go to your Cpanel and create a subdomain like “cdn.yoursite.com” and forward /point this domain to www.yoursite.com/wp-content/uploads
2. Install “WP Original Media Path” plugin
If you are using the old WordPress i.e. version less than 3.6, then you don’t need to install this plugin. But if you are running wordpress version 3.6.1 or higher you need to install this plugin as never version of WordPress doesn’t have this option in it. Download the plugin and activate it.
3. Change the Image Upload location in wordpress
Go to Settings > Media
There you will see two options under “Uploading Files”
a) Store uploads in this folder – delete everything in it
b) Full URL path to files – use “http://cdn.yoursite.com” there.
All your website image uploads will still be uploaded in the default location i.e. “wp-content/uploads”, it’s only that we will call them through a subdomain now.
4. Edit Database
Now the database needs to be edited so that the old posts will also call the images through the subdomain. If you do it individually, it will be a hectic task.
We will make it easy.
Go to Cpanel > PhpMyAdmin > your wordpress database > SQL
Here you will need to run a query in it so that all older posts will change their image url to the new created subdomain.
You will need to paste the following code in the box and press GO
UPDATE wp_posts SET post_content = REPLACE(post_content,'http://www.yourdomain.com/wp-content/uploads/', 'http://cdn.yourdomain.com/')
That’s it. You have successfully hosted your images in a subdomain and increased the page speed by about 20 – 30%. It also helps in increasing the rankings of your website in Google.
If you feel that this is beyond your abilities you might like to check this post to call images from other website for free.