This is a guest post by Yousuf who blogs at HaveFunForever. If you would like to write for ShoutMeLoud, check our guest posting guidelines.
We all know that Google started ranking website based on Page speed. Though the difference due to page speed could be as low as 1%, but it’s one of the part of Google SEO suggestion. In this post I’m going to share how you can host images in a sub-domain and server images to main blog.
Also Read: How to Lazyload images to improve site loading speed.
Earlier I shared how to use dropbox as CDN, but using Dropbox was a little manual so I wanted something automatic which may also improve my site speed. After searching the web, I found that serving images from a sub-domain also improves the loading time because of the parallel downloading ability of a browser. In this post I will tell you how to serve images from a sub-domain and also redirect the older ones to the new ones.
Creating A Sub-domain
Log in to your web-hosts CPanel, scroll down and look for sub-domains under domains and click on it.
Enter a name for the sub-domain i.e img.example.com, give the path of the uploads folder which is usually public_html/wp-content/uploads and click on create.
Setting a Sub-Domain in WordPress
Using a sub-domain is really easy. Go to Settings >> Media and enter the sub-domain in the Full URL path to files with http in the beginning and no trailing slash (/) in the end. Click on save changes. Look the image for more clarification.
You have set up sub domain for WordPress images but the older posts still use the complete URL. They can be changed to the sub-domain using the following SQL query. First open phpMyAdmin.
Click on SQL from the nav-bar and enter the query given below, then click on Go. Make sure to replace example.com with your actual domain.
UPDATE wp_posts SET post_content = REPLACE(post_content,'http://www.example.com/wp-content/uploads/','http://img.example.com/')
Redirecting Old Images to the new one
Other pages on the Web and Search engines might have indexed your images so it is better to redirect them to the new images which are hosted on sub-domain. All you have to do is enter the following code in the .htaccess file and save it. If .htaccess file isn’t available then create one.
RedirectMatch 301 ^/wp-content/uploads/(.*)$ http://img.example.com/$1
Make sure to replace example.com with your actual domain.
I hope this tutorial helped you in improving you page speed. Do check your website’s speed using these tools and tell us about your new load time.
Do let us know if you prefer serving images from main domain or you also create a sub-domain to server images & files?
Subscribe Updates, Its FREE!

![How to Serve Images from a Sub Domain [WordPress]](http://www.shoutmeloud.com/wp-content/uploads/2011/10/subdomain-making.png?699e3a)
![How to Serve Images from a Sub Domain [WordPress]](http://www.shoutmeloud.com/wp-content/uploads/2011/10/enter-subdomain-name-600x113.png?699e3a)
![How to Serve Images from a Sub Domain [WordPress]](http://www.shoutmeloud.com/wp-content/uploads/2011/10/enter-subdomain-in-wordpress-600x379.png?699e3a)
![How to Serve Images from a Sub Domain [WordPress]](http://www.shoutmeloud.com/wp-content/uploads/2011/10/php-my-admin.png?699e3a)
![How to Serve Images from a Sub Domain [WordPress]](http://www.shoutmeloud.com/wp-content/uploads/2011/10/run-my-sql-query-600x379.png?699e3a)




→
{ 17 comments… read them below or add one }
using sub domain for hosting images have its own flaws.., wordpress custom functions are not working well with the images which are hosted in subdomain, I have checked them in my site..
Nice idea and explained properly.
I prefer to deliver images and media from subdomain, which definitely improve the speed and page speed test scores by reducing the requests from same domain name.
Thanks for the share and I will try this very soon.
i create but the following error found…..
404 Not Found
The server can not find the requested page:
pic.mukesh.co/2011/10/Pumpkin-Patch1.jpg (port 80)
Please forward this error screen to pic.mukesh.co’s WebMaster.
Thanks for a great tip and explained so well. Did your Page Speed score improve by 1%, i.e. does the test recognise this as a CDN?
An Informative article Yousuf. Serving WordPress Uploads from a Sub Domain seems to be an good Idea. Is there any way to serve Images and Videos from two different Sub Domains?
Cool tip. I heard if we can serve uploaded contents from 4 different subdomains will improve speed alot more as compared single subdomain. How we can set up the same? Will you please explain?
Quick tutorial there, can you please create something similar related to CDN. I’m thinking to use it in near future but my concepts aren’t that clear about it.
well, nice idea to speed up a blog, but have a doubt that do we have to copy “upload” folder or previous images from main domain to a new sub domain?
in my case the sql query to change the location does not change any thing in my database thus leave me in the same position all the time. I am using wordpress 3.1.14. ANy idea what to do now?
Not working man, I have started a new blog, so the Re-direction you have mentioned won’t be useful, so I can manage images on a sub domain from starting, I am trying this but doesn’t works man.
I would like to know how to do this on wordpress multi-site where the upload settings options that you mention are not available in the blogs dashboard. Please advise.
do this work on new vertion (3.5) ?? or no??
can i use
RedirectMatch 301 http://www.techntrix.com/wp-content/uploads/(.*)$ http://img.techntrix.com/$1 in stead of RedirectMatch 301 ^/wp-content/uploads/(.*)$ http://img.techntrix.com/$1 ????? because i have domain http://www.versedtech.org in public_html/versedtech. if i follow your one 301 redirect i.e. RedirectMatch 301 ^/wp-content/uploads/(.*)$ http://img.techntrix.com/$1 then it shows broken image at versedtech. but if i use RedirectMatch 301 http://www.techntrix.com/wp-content/uploads/(.*)$ http://img.techntrix.com/$1.
can i use my method for this purpose?
Great, But i got a problem, when i am doing this process WordPress 3.5.1 does not support Media Redirection, So please help me for solving this Problem
Use wordpress original media path plugin to bypass your problem with 3.5.1
This plugin is a one time use plugin. You can delete the plugin after changing default media location. Here is the link: http://wordpress.org/extend/plugins/wp-original-media-path/
else just go to http://www.domain.com/wp-admin/options.php and update the values of
upload_path
upload_url_path
instead of installing plugins.
After i follow this tutorial, I got 404 error for my sub domain. What could be the reason? Any idea?