Host Images of WordPress Blog in a Subdomain for Better Speed

Posted by Tanmay

In some previous articles, I’ve described about how to reduce the page load time of your WordPress blog [ 1. Speed Up Your WordPress Blog By Improving Page Load Time 2. Install Contact Page in WordPress Blog Without Any Plugin3. Reduce Page Load Time Installing Tweetmeme Retweet Button Manually ] .  Another issue is still yet to discuss that effectively participate in your page performance. By default the images of your site are hosted in the same domain. This slow downs your page speed. Also read: How to Add Extra Widget Section In WordPress Blog. Because, almost all the browsers create maximum two-four connections when downloading a page from the web server. So if your pages contain more than one images then it is wise to host those images in another domain so that the browser can download the images in parallel with your main domain. WordPress does not support hosting images in different domain. But fortunately, it supports and allows you to host images in a subdomain. This article is about how to host your website post images in a subdomain along with the old images that were hosted in the main domain.

Create a Subdomain for your site

In order to get your subdomain you’ve to log-in to your domain control panel. Point to Subdomains and create your subdoamain, provided your domain resides under /public_html/. Note: The facility depends on your hosting provider. So if you are unable to create a subdomain then contact your hosting provider.

Change default image  uploading folder in WordPress

This step involves in setting WordPress to change the default uploading folder for the post images. log-in to your WordPress dashboard. Click on “Miscellaneous” “Media” under “Settings” from left side of the dashboard.

Fill up the fields as follows: Store uploads in this folder : images Full URL path to files : http://images.yourdomain.com

Click on “Save Changes”. Now whenever you will upload any image to your post, the images will be uploaded in the subdomain http://images.yourdomain.com.

Update the old post images location

You’ve just changed the upload location of your post images. So the images of the new posts will be uploaded in the subdomain. But what about the old post images? Changing the location of all the old images one by one is a stupid job. So at first download all the folders containing the images under /public_html/wp-content/uploads/ from your server. Upload all those folders in the folder /public_html/images/. Now you have to execute a simple SQL query on your database. You can do this from the phpMyAdmin in your domain control panel. Click on phpMyAdmin > select your database from the left side > click on the “SQL” tab. Now put the following query in the box

UPDATE wp_posts SET post_content = REPLACE(post_content,'http://www.yourdomain.com/wp-content/uploads/','http://images.yourdomain.com/')

Click on “GO”. This query will replace “http://www.yourdomain.com/wp-content/uploads/”  with “http://images.yourdomain.com/” in all the image location url. To update the images links in the media library run the following sql command

UPDATE wp_posts SET guid = REPLACE(guid,'http://www.yourdomain.com/wp-content/uploads/','http://images.yourdomain.com/')

Redirect the old images link to to subdomain

You have completed all the task with your site and database. But Google has crawled and indexed your old images. So redirect the old image urls to the new image urls to let Google know that those images are same. To do so ad the following line in your .htaccess file

RedirectMatch 301 ^/wp-content/uploads/(.*)$ http://images.yourdomain.com/$1

Reminder: Backup your .htaccess file before any modification. That’s all to set your subdomain as post-images upload location. Any question is always well come. Put your feedback in the comment section.

This post was written by Tanmay

Reader Comments

  1. this is a nice trick to reduce the load time, worth remembering :)

    • Tanmay

      Thanks Shekhar.

  2. Amy

    this is a nice trick to reduce the load time, worth remembering :)

  3. Sariyanta

    Hi Tanmay, this is a good article, and i apply it already on my website http://www.rokocountrybarak.co.cc
    By the way, can i also put my image themes in my subdomain? and what should i do in order to make everything correct. I want to do this because my theme is kind of heavy theme

  4. Sariyanta

    sorry, i forgot to say the important word: Thank you very much for the nice trick..haha

  5. Tanmay

    @Sariyanta
    Happy to know that you found our article useful. Thanks for your nice words. By the way I could not understand what you’ve wanted to do. Do you want to keep the theme in subdomain or the theme images?

  6. Sariyanta

    That is fast response, thank you, i appreciate your attention.
    What i intend to do is to move my image theme into sub domain.
    One more problem, when i try to upload an image to my subdomain, it showing something like this
    The uploaded file could not be moved to /home/xxxxxx/public_html/img/2010/06. and my subdomain is img.mydomain..
    can you help me?
    thanks for your time

  7. Sariyanta

    sorry, everything is ok now, except the image themes one

  8. Sariyanta

    one more thing, what about css and js file? do you have an explanation about this?

  9. Sariyanta

    Hi again Tanmay, i want to move all the theme in subdomain, and also i want to create another subdomain to store my script file, is that possible? if its possible then how to do it? i’ve tried googling but nothing makes sense to me, i am a newbie, so this kind of article with a complete tutorial will be very helpful.
    thank you.

  10. Tanmay

    @Sariyanta,
    No, you can’t upload the theme in a subdomain. But obviously you can move some theme templates in the subdomain and call them properly from your main theme templates. The same can be applied for the css and javascript templates.

  11. Sariyanta

    Okay, thats sounds good, so now i want to host my css and js in my sub domain, how should i call it? which file should i edit? sorry i’m newbie and asking so much question

  12. Tanmay

    Put the following
    link rel=”stylesheet” href=”your style-sheet location” type=”text/css” media=”screen” />
    replacing the existing one in the header.php.
    Similarly, apply that for javascripts location in that same template header.php.

    Never mind about that. Feel free to ask any question. We are happy to help you. You can subscribe to our RSS feed or Daily news letters from the top of the site. Keep coming.

  13. BTW I use picasa, Google has the fastest server :)

  14. Tanmay

    @Shekhar
    Frankly, I never tried picasa. I think saving assets in own server is always trusty. Thanks for your comment.

  15. Nice tutorial! I’m going to migrate my old blog to the new one. It helps me to make it easier.

    • Tanmay

      @Gojeg,
      Thaks. Keep reading our tutorials.

  16. Really interesting post. I have only just started using WP3 Cache to try and improve my page speed due to sudden increase in traffic however, if that doesn’t turn out well, I will give your images sub-domain suggestion a try and feedback to you.

    • Tanmay

      @Jay,
      Thanks for your appreciation.

  17. Nice, really good tutorial thanks a lot!

    • @Swapnil: Thank you for your appreciation.

  18. Wonder how this will work on a Multi-Network site using JJJ’s plugin…

    http://wordpress.org/extend/plugins/wp-multi-network/

    Think I’ll test it on the dummy network.

  19. @Eddi Hughes: Frankly, I didn’t test in Multi-network site. But hope it will work there properly. Feel free to let us know after testing in your dummy network. Thanks for sharing.

  20. xyzzy

    any idea how to do this for WPMU (WP 3 Network install)?

  21. lemmekillmister

    Hi Tanmay,

    I have implemented this subdomain and the CDN on a site that i built but right now, i am having a problem with the https.
    When the site is visited through https, i have all my images and javascripts pointing to http://images.demi.com/web/images/x.gif.

    The site loads but with an alert in IE asking for user confirmation because it has both secure and unsecure data on the web page.

    My question to you would be , is there an easy way to get around this problem and let the browser use https when it is https and http otherwise.. ( a relative url something starting with images.demi.com ,without specifying the entire path starting with http)

    I tried this and it does not work
    <img src="images.demi.com/web/images/x.gif" …
    but this works with http
    <img src="http://images.demi.com/web/images/x.gif&quot; …

    Any help is really appreciated. Thanks in advance
    Lemme

  22. Tanmay

    @Lemmekillmis: Thanks for putting your idea here. By the way, I don’t know even if WordPress supports both http and https at the same address. But I’ll make a research on it. Thanks again for sharing. Hope I can help you on this matter.

  23. Tanmay -

    Thank you for your post! I was actually searching for another topic and discovered this. I think that I will implement this on a less important site to ensure I understand everything. Once I do, I will implement on my main site, http://www.AllAboutGratitude.com .

    Is there anyway to benchmark before and after load times (easily)? I have found that some plug-ins are the real culprits of degradation of page load times so I have deleted them. I should probably delete a couple still!

    Again, thank you. Sorry for the long comments.

    Be Well.
    Paul.

  24. Hi how to do this in wordpress 3

    • Tanmay

      The procedure for WP 3 is same as mentioned in this post. Follow the instructions and let me know if any problem arises.

  25. This is a great tip! Works like a charm for 3.0.1. However when creating a child directory within the subdomain, the flash uploader is not recognizing the child directory? it creates the images directory within the wordpress files. Any ideas?

    Store uploads in this folder: images/directory-name
    Full URL path to files: http://images.mydomainname.com/directory-name

  26. Tanmay

    @Gymgangsta: did you check the file permission for that directory?

  27. Hi Tanmay,

    I did check permissions, made the directory 777, no luck. I did not check the permissions for the subdomain “images” you think that is the problem? My paths are correct right?

    cheers for the fast response and for your code above, it is really a timesaver…

  28. Tanmay

    I think some thing wrong that is preventing the image directory recolonization. I also use this method for this blog and working fine. But how can I find problem with out seeing the code? Can you send a screen shot of the media setting? You can upload that from http://www.techtipsgeek.com/go/up/ . Feel free to ask more question.

  29. i listed my settings above: here it is again.

    Store uploads in this folder: images/directory-name

    Full URL path to files: http://images.mydomainname.com/directory-name

  30. Tanmay

    I can’t understand what the hell is going on. You can try the following

    1. Type the full location of the folder at “Store uploads….files:” like /public_html/images/your-dir
    2. log out from WP and then log-in again. Then try to upload.

    If those does not solve then you can submit you site to our service section. http://www.techtipsgeek.com/services/ . Don’t worry, it is free for you.

  31. Thanks Tanmay, I’m quite savvy with wordpress, and this stumps me too. I’ve tried a few things, letting wordpress create the directory (so it has ownership) etc, I’ve tried various path combos, I’ll try your suggestions tomorrow when i am back at the grind again.

    cheers! =)

  32. Tanmay

    Its okay man. No problem. Let me know what solves the problem.

  33. Hi Tanmay. I tinkered with it some more and am thinking this. My wordpress is installed in a subdirectory (www.mydomain.com/wordpress/

    and no matter what path I set for images to my subdomain (images.mydomain.com) it wont show up. I’m thinking it’s an .htaccess problem and not a permissions problem. I’ve got some crazy stuff in my htaccess. So i’m gonna just upload these images into the content/uplaods folder then move them manually and specify the correct link.

    =)

  34. Tanmay

    @Gymgangsta: you can read this topic http://wordpress.org/support/topic/upload-error-5

  35. thanks mate :) am hunting for this tip ..at last i found it :)

  36. Tanmay

    Congrats! Just apply this in your site and let me know about your experience.

  37. thanks for the wonderful tip, it saved my time and website performance

  38. Tanmay

    Glad that you liked. Thanks for the comment.

  39. Hi again Tanmay, on a side note…. I did the same thing for videos. I created a http://videos.mydomain.com and noticed my 404 error logs (in the redirection wordpress plugin log) are reporting /crossdomain.xml

    further investigation leads me to a confusing doc: http://kb2.adobe.com/cps/142/tn_14213.html

    maybe you could write an article on what would be the perfect code for the crossdomain.xml and exactly where it goes.

    I gather the file crossdomain.xml resides at the root of http://www.mydomain.com so that it can serve up flash video from the subdomain: http://videos.mydomain.com

    the code would be:

    cheers !

  40. Jim

    Well written sir.

    Of course the other alternative is to host all images on a free hosting site like image shack, flickr or Picasa, and referencing them from the blog. Two benefits are that it allows me to avoid bandwidth and relies on their server speeds.

    Can you comment on the benefits and concerns of a subdomain vs. a remote host?

  41. Tanmay

    Jim: Your points are really appreciating. But they all end at one question. How much do you depend on a third party service about the security issue of your assets?

  42. Jim

    Good point Tanmay. Although if Flickr or Google has security issues, there will be a much larger issue than just my blog! haha.

    Actually, I almost always use others’ pictures available in the Commons to illustrate my posts, so my personal pictures are not in general at risk.

  43. Kunal

    @gymgangsta:- the problem is in your image upload file path.

    If you are on shared host,you need to use the entire path right from /acc/public_html……..etc

    Hope this helps.

    BTW,thanks Tanmay :)

  44. oh ! i have a big traffic , i have a magazine so upload to other host is the great Idear ! say thanks to U

  45. How to get this back on default?

  46. Tanmay

    John, just delete the paths that you’ve provided under the Miscellaneous settings.

  47. Hi Tanmay, great article.

    One question, if i want to store my data on another domain (not-subdomain), how should i set uploading setting from WP?

    I mean instead of having http://static.domain.com/img/xyz.jpg
    i want to use, http://www.staticdomain.com/img/xyz.jpg

    I’ve read some articles and realized that another domain option is way much better.

    What do you think?

  48. Tanmay

    Mustafa: If you are in WordPress then it does not allow to host images/videos in different domains.
    What You have read about the different domain is hosting your assets in different domain boosts your blog performance. And the subdomain works like a different domain in that very purpose.
    You cam also host your images from some Content delivery network (CDN). But there also, the domain name should like a subdomain. e. g cdn.amazon.com.

    Atlast,
    if you want not to use subdomain then host them in another domain and insert the link in your WOrdPress blog. But you will not be able to upload or manage those images/videos from your WordPress blog (main domain).

  49. Dear Tanmay, thanks a lot for such a quick reply.

    Best regards.

  50. Tanmay

    You are welcome Mustafa..

  51. Hi, nice sharing…
    I have blog about Celebrity Gossip and I need to host media library in other hosting.
    Please tell me how to configure it.

    Thanks

  52. laptopy

    I was suggested this web site by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my problem. You’re amazing! Thanks!

  53. Prishtine

    Thank you so much with regard to giving me an update on this topic on your site. Please know that if a brand-new post becomes available or if any improvements occur about the current post, I would be thinking about reading a lot more and learning how to make good usage of those strategies you talk about. Thanks for your time and consideration of others by making this web site available.

  54. Decio

    I’m reading about this issue on google, but it seems to me it would be easier to use the Supercache plugin and cache it somewhere else via CDN. Less hassle, wouldn’t it?

  55. Tanmay

    Decio: Yes, you can setup this via Supercache plugin. Many people do not use Supercache or such plugin. This method should help them a lot.

  56. Hello Tanmay,
    i have question. i have two domain for say domain1.com and domain2.com if i upload all my images at domain2.com and call images from there for domain1.com then would domain2 will have any benefit for this

  57. remember : to be the best for SEO you can host images in a subdomain –> not other domain e.g images.mmm.com (not images-name.com and your site is eg.com)
    When a visitor come to see your images in images search or someone copy your images – –> it;s been count for your site ….

  58. Tanmay

    In this case all your images will be indexed in Google with domain2. But I think that will not benefit domain2 so much as Google does not like the domain without any text.

  59. @Tanmay will this affect my web page speed

  60. Tanmay

    Yes, this will reduce the number of HTTP requests in domain1

  61. will it slow down loading speed of domain 1 if i host both the domain at one host only.

    • Tanmay

      No…

  62. Ankit

    Thanks for all you’re help

  63. Ankit

    hey tanmay where do i need to Change default image uploading folder in WordPress version 3.1.1

  64. Tanmay

    It is same as the previous version of WordPress. Follow the article.

  65. Ankit

    Hey tanmay i am still unable to upload the images on my blog as it is uploading on my old domain. i have change the required settings exactly as you told. although now my blog is calling all the images from my new domain but unable to upload it on new domain.
    if possible can you please tell me how to change settings from functions.php what i need to change in functions.php so that all my images uploads from my new domain………

  66. Ankit

    my wordpress version is 3.1.1

  67. Tanmay

    Sorry Ankit. I forgot to update the article. Go to Settings > Media instead of Miscellaneous. I also updated this post. Now follow and let me know if you are able to upload your images to your subdomain.

  68. Ankur

    Hi Tanmay i currently have a website hosted on server that are almost private so no problem regarding speed and other issue but there is limitation regarding webspace i have only 1 GB, is there a way to host my files on different host (No CDN service like amazon) i mean like free webhosting out there, is there a way to use them..

  69. Decio

    @Ankur
    You can host you entire site for free on google app engine. Search for google app engine static and follow this tutorial:
    http://www.labnol.org/internet/host-website-on-google-app-engine/18801/

  70. Li-An

    What about SEO ? I manage a blog based on images and accept hotlinking as Google count them as valid link to my site. What will happen if I move them to subdomain ?

  71. Hi nice post. Recently i’m thinking to do it.

  72. Andre

    Hi Tanmay. I’m not sure if this is possible with WP, but if read/write permissions are not an issue, could this method be used to get WP to store images on a completely different server through a different URL?

  73. Tanmay

    WP means WordPress? If so then this tutorial is completely working for WordPress. This tutorial only allows you to host images to a subdomain of your main domain.

  74. Andre

    Yes, my apologies, I generally refer to WordPress as WP, makes it easier to write.. Thanks for the prompt response, I guess I’ll have to keep hunting for a plugin to do this then. I’m sure it must be possible somehow, I just haven’t found out how yet…

  75. Tanmay

    @Andre: If you really want to host your images in different domain Try some CDN like Amazon : http://aws.amazon.com/cloudfront/

  76. Andre

    Thanks Tanmay, this was something my brother asked me to look into. He was asking me if WordPress was capable of using photos he currently has in some Google-based storage (I think he meant Google Docs, but I’m not sure). So when I found this blog post I thought I’d at least ask and see.

  77. Very nice post dear

    thanks for the help… :)

  78. I’ve been looking for an idea like this. I was going back & forth between hosting images on the domain, and using a image hosting service. I’m giving this a try next, thanks for the tip! :)

  79. Thanks for the detailed guide.. i am planning to change my webhost next month and decided to move images to a sub domain.., mu query is whether to host images in the same web server or use a CDN network for hosting images??

  80. Excellent simple tip.

  81. umang

    Nice post.
    Well, i create a subdomain for image hosting, like images.Google.com. How to prevent access the image hosted subdomain?

  82. zaadu

    I wasn’t aware of this tip..I will give it a try and let you know

  83. Gud one! but I am serving my images via CDN

  84. Amar

    Hi Thanks for tips. its really cool.I have configured everything as mention in your post. I am not having issue with old images but while i uploaded new images it went to old wp-content/uploads directory. I would like to mention here that i have configured ” Full URL path to files” as instructed in post. While full url path is set then why images are getting upload in default directory? Please help me.

  85. Amar

    HI. My problem has been solved. Thanks. I must say your writing is simple and complete and I really appreciate that. Most of tips or tricks available on net are half written or sometimes i do not get idea that what writer asking me to do. Labnol is one of finest technical blog but while you read-how to setup image folder for wordpress, you lost some where because of half written article. Please continue simple and complete writing. Thanks

  86. Amar

    Hi, I am facing problem with CDN. Now my images are not showing in post. Please help me ? I am using rackspace CDN.

  87. Anonymous

    I was made all like in this post, and everything work ok, but i can’t upload in new subdomain. How to solve that?

    • Anonymous

      Did you made the changes at your WordPress settngs > media?

  88. This is a great trick and you have explained it well. Thanks so much!

  89. Really great tips.
    Thanks for share.

  90. Very informative tutorial. But I have heard that if you save uploaded contents on a subdomain, cache plug-ins (W3 Edge Cache, WP Super Cache, etc.) won’t work. Is this solved already?

  91. Blaine

    Thanks for the detailed instructions.
    Is there any reason to keep checked, “Organize my uploads into month- and year-based folders”? I figure the less folder names in the path, the better for SEO.

  92. Hi, thanks for this post.

    Been really struggling trying to set this up with my host (123 Reg).

    They are basically saying that what I’m trying to achieve (which is what this post is about) is impossible.

    I’ve purchased a subdomain from them, and when I follow the guidelines set out in this article, I get a SQL error message

    “The this error pos up at the top : #1146 – Table ‘aminearlythereyetco_837484_whatever..’ doesn’t exist”

    They have provided me with a work around, but I’m not sure whether it’s the right thing to do… this is what they have proposed..

    “You can load you images into a folder on your hosting package and make that url (aminearlythereyet.com/folder/photo.jpg or aminearlythereyet.com/folder) live. Then you will have to create a framed fprwarding rule in order to forward your subdomain to that specific url by following the steps from this link: How do I add 302 (Temporary) Web forwarding?
    Also the media.aminearlythereyet.com subdomain will have to be pointed to the webforwarding IP address 94.136.40.82 by following the next steps:
    log into your control panel, select the aminearlythereyet.com domain from the drop-down list and click “Manage”
    click on “Manage DNS” button an then on the “Advanced DNS” tab
    point the “media” record to 94.136.40.82 instead of 94.136.40.103
    Please note that any DNS change needs time to propagate 24-48 hours. This way when you will access media.aminearlythereyet.com you will be forwarded to aminearlythereyet.com/folder/photo.jpg and the url in the web browser will be media.aminearlythereyet.com.”

    Any advice would be really helpful. Thank you very much!
    Stuart

  93. Hello and Thank You for this great post. I followed your directions and it seems some of my images are updated and working and others will not work. I have even tried uploading the images from pc and replacing them with the updated image and they will not appear. I am lost and could certainly use someones advice or help. My pages are a mess now and I dread manually recreating everything. I’m sure there is a painless way to fix my problem. If anyone with more database experience would give me a hand, it will be greatly appreciated. Thanks in advance for any support.