Host images in a subdomain

By default, most Web browser will create from two to four connections to the Web server, when downloading a Web page. This will results in slow Web page displaying, especially if the Web page contains more than one images. However, we can actually trick the Web browser, by hosting the image in other domain, such as Flickr or Picasa Web Albums. Therefore, the Web browser will download the images in parallel with the rest of the Web pages.

Suppose that you had, or subscribe to a lot of space and fast Web server. You can actually self host the images. The image will be hosted in a subdomain. For example, if your blog domain is http://yourdomain, the images could be served from http://images.yourdomain. Thus, this article will show you how to accomplished it.

Create the subdomain

In order to create the subdomain, you can use the cPanel or DirectAdmin, whichever you have, or via SSH, if you are a geek. Point the subdomain to the default WordPress uploads directory, which is /public_html/wp-content/uploads, provided that your WordPress installation is in /public_html directory.

Create subdomain using cPanel.

Create subdomain using cPanel.

Update WordPress Miscellaneous Settings

The next step is to instruct WordPress, where the upload files should be located. Go to the WordPress dashboard. Click on Miscellaneous under the Settings menu. Leave the first text box as is. By default, the second text box is empty. Fill it with the full URL path to files, which is http://images.mydomain.com.

Update WordPress Miscellaneous Settings.

Update WordPress Miscellaneous Settings.

Now, whenever you upload new images, in a post, the new images will be uploaded into the new subdomain. Although, those images will actually will be located in the same directory as before, /public_html/wp-content/uploads.

Update MySql for all previous images

Up until now, you have successfully make WordPress to host images in a subdomain, but what about all the hundreds or thousands of images that you had posted previously. Fortunately, WordPress save the images information in the database, in pure text. Therefore, just use simple sql to change the images information, for example, from http://yourdomain/wp-content/uploads/2009/01/mypic.jpg to http://images.yourdomain/wp-content/uploads/2009/01/mypic.jpg.

Using phpMyAdmin in cPanel or DirectAdmin, or any other MySql client, connect to your WordPress database. Beforehand, remember to backup the database. That way, you will have a failsafe mechanism.

To update the images links the post contents, run this sql, after you have back up the database.

UPDATE `wp_posts`
SET `post_content` = REPLACE(
`post_content`,
"http://yourdomain/wp-content/uploads/",
"http://images.yourdomain/"
)

To update the images links in the Media Library, run this sql. This step is not compulsory. Just to remind you that you should back up the database first.

UPDATE `wp_posts`
SET `guid` = REPLACE(
`guid`,
"http://yourdomain/wp-content/uploads/",
"http://images.yourdomain/")

Redirect all the images links to the new sub domain

The last step, is to redirect all the previous posted images links to the new URLs. Duplicate links will make Google unhappy.

First, Back up your .htaccess file, which is located in public_html directory. Then, prepend a RedirectMatch code that will redirect all images to the new subdomain. This is one way to accomplished it.

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

In conclusion, you have successfully trick the Web browser, by redirecting your images into a subdomain. Now, the Web browser will download images from you Web server in parallel. If you are using a cache plugin, please clean all the cache, as well as the Web browser’s cache, in order to see the changes. Last but not least, please back up the database and the .htaccess file, before committing any changes.

Related

This entry was posted in WordPress and tagged , , . Bookmark the permalink.

26 Responses to Host images in a subdomain

  1. KNizam says:

    terlampau technical untuk aku. hehe

  2. Selinap says:

    I thought that you’re an engineer.

  3. exinco says:

    it is valuable if you include screen shot for every step

  4. Selinap says:

    I’ll think about it. Thanks.

  5. KNizam says:

    hehe nope. i’m accounting/finance…hehe :)

  6. Hey must say it’s a great tip. Hosting images in sub domain will reduce the concurrent connection and thus will make your wordpress blog fast. Kudos to this useful post.

  7. Pingback: Speed up a wordpress website « My Blog

  8. koole says:

    then how to point the upload path to other server/IP?
    say i have domain.com, and i want to upload images to img.domain.com which has different ip address.
    please advice,
    thanks!

  9. Selinap says:

    @koole: The objective of this tutorial is to host the images in a subdomain. In other words, the images will actually be in the same domain as WordPress.

    BTW, for your case, there are multiple options. Here are some of them, that I can know.
    1. Use FTP client to upload the images manually to FTP server. When writing a new post, click “Add an image” icon, and choose From URL to insert the image into your post.
    2. Use desktop blogging software such as Windows Live Writer and w.blogger. Using these softwares, you can specify to upload your images to FTP server instead of WordPress.

  10. koole says:

    Hi there,
    thanks for the options, but that sounds so ‘manual’ :D
    i’m wondering if i can upload all those files/pictures automatically to the other server/subdomain, because i have one ‘idle’ webhosting account here :)

    Thanks anyway, nice blog! :)

  11. Selinap says:

    @koole: Option number 2, which is using WLW or w.blogger will upload images to the other server automatically whenever you post, just like normal WordPress image upload.

    I suggest that you give those software a try.

  12. Gigi says:

    Thanks a lot Selinap for those sql code, i didn’t know how to change all already posted images in my articles.
    Sincerely
    Gigi

  13. mehmet says:

    it is working but thumbnail effect stopped working?

  14. Selinap says:

    @mehmet: It won’t affect the thumbnail effect.

  15. Margarida says:

    Why point the subdomain to the default WordPress uploads directory, /public_html/wp-content/uploads??
    If you create a subdomain http://images.yourdomain, wouldn’t it be more logical to point it to a folder called images? Would this work just the same?

  16. Janey says:

    You forgot to cover DNS & Apache. If you just make “images.blah.com” and there’s no appropriate DNS entry for it, it’s not going to work.

  17. Selinap says:

    @Janey: In this tutorial, the sub-domain will point to a directory internally. Hence, no need to set the DNS.

  18. P.K.ARUN says:

    Hello selinap please help me, after implementing this on my site (ashout.com) all my images are moved to sub domain (img.ashout.com) and I also changed the images uploading path in wp dashboard (Miscellaneous Settings). Now when I try to upload images in new post it is uploading properly to correct path but it is not displaying in posts. When I check that images URL it is not showing images just giving 404 error means it is not uploading any images to the new path.
    Please help me selinap.

  19. Selinap says:

    @P.K.ARUN: It seems like you should use http://www.img.ashout.com instead of img.ashout.com for your case, since img.ashout.com is automatically redirected to http://www.img.ashout.com.

  20. P.K.ARUN says:

    Thanks for replying,

    I used the same, can please tell me one thing, I uploaded all images to http://www.img.ashout.com/Images/ so what value we need to fill in the wp-admin–>> settings–>>Miscellaneous Settings–>

    Store uploads in this folder: ??what value i need to put here??

    Full URL path to files: ??what value i need to put here??

    and give the screenshot of urs what u have put in those field (if possible) I’m really scared after attempting steps to my blog :(

  21. Selinap says:

    @P.K.ARUN:

    Store uploads in this folder: This is the folder where you want to upload the images. For example, if you want to keep the original upload directory, then use wp-content/uploads.

    Full URL path to files: This is where you define the sub-domain that point to the uploads directory, which you have defined in Store uploads in this folder:.

  22. P.K.ARUN says:

    Thanks for replay, Seems it doesn’t work for me :( now I revert all images back to main domain.

  23. Lisa says:

    Thank you. I tried this, but am now having 2 problems.

    First I can no longer upload new images in wordpress.

    Second, I keep on getting the following error on the homepage

    Warning: is_dir() [function.is-dir]: open_basedir restriction in effect. File(/) is not within the allowed path(s): (/home/site:/usr/lib/php:/usr/local/lib/php:/tmp) in /home/site/public_html/wp-includes/functions.php on line 2037

  24. Selinap says:

    @Lisa: Try to use

    /home/site/public_html/wp-content/uploads

    instead of

    /public_html/wp-content/uploads

    for the document root.

  25. Awesome idea to speed up website by reducing the connections to the server :-)

    Just done this to my blog http://www.aspiredtechie.com by creating a subdomain ‘cdn’ for it.

    Thanks for the tip, Selinap!

  26. Selinap says:

    @Sagar Gholap: You are welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>