Hosting images of your site in another subdomain is one of the best ways to improve the loading speed of your website. Last week, I spend some hours (or minutes) doing this.
If your pages contain more than one image then you should host those images in another domain. By this, the browser will be able to download those images in parallel with your main domain. It will reduce the concurrent connection.
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 Keeping The Images
To create a subdomain you need to use your domain control panel. The procedure for creating a subdomain depends on respective hosting provider; normally it is not a tedious task.
You can create your desired subdomain. But image or images or img recommended! 🙂 Create a folder with name “Images”.
Change The Default Image Uploads Folder In WordPress
Log-in to your WordPress dashboard. Now go to Settings | Miscellaneous. For WordPress 3.0.1, Go to Settings | Media.
It should be like this,
Store uploads in this folder : Images
Full URL path to files : http://img.yourdomain.com
Click on Save Changes. From now onwards, whenever you upload images, it will be stored in the subdomain img.yourdomain.com
Update The Old Images Location
Now you have updated the upload location of your post images, but what about the old post images? You can’t change the location of all the old images one by one. That’s a tedious task.
First download all the images in the folder /uploads/
and upload them into the folder Images.
Now we have to perform a simple SQL query on your database. You need to use your phpMyAdmin for executing this.
Go to phpMyAdmin and the click on the SQL tab.
Now put the following query in the box
UPDATE wp_posts SET guid = REPLACE(guid,’http://www.yourdomain.com/wp-content/uploads/’,’http://images.yourdomain.com/’)
Click on GO. This query is capable of replacing “http://www.yourdomain.com/wp-content/uploads/” with “http://img.yourdomain.com/” in all the images location URLs.
Hurray! Now you have completed the process almost 95%. Considering the images Google crawled and indexed, we have to redirect the old image URLs to new image URLs.
In order to do that, add the following line in your .htaccess file (What is .htaccess?)
P.S. Backup your .htaccess file before implementing.
RedirectMatch 301 ^/wp-content/uploads/(.*)$ http://images.yourdomain.com/$1
Now you are done, perfect!
You can subscribe to our feeds to get quick updates about such tutorials!
Getting any error sort of thing? Do you know any other tip? You are welcome to share!
Yes this may improve your loading speed,but your server is still loaded even if try to fetch the images from subdomain !
I decided to go back to the way it was and deleted the sub domain and changed the media settings to the way it was and now none of my images is loading. I can see them in the media section and on cpanel but when I load the site, no images is being loaded.
Its all messed up. And my create new post is also messed up. I see that your entire post is a poor copy paste job from other sites who have written the article way before you did.
You did not considered the other factors like wordpress updates and other plugin issues. No thought was given whatsoever. Its a word by word copy paste job.
This is sad. Wasted several hours to find out and now will waste more hours to fix it.
Its not working on wordpress version 3.5.1 Here some screenshots for various steps and settings. For phpmyadmin I tried two different codes both resulting in same results. Screenshots for both are here.
I just uploaded the screenshots in my new sub-domain but it is not visible at sub.domain.com
takeaclick.com
img.takeaclick.com
http://img.takeaclick.com/cpanel-sub-domain.JPG
http://img.takeaclick.com/php-my-admin.JPG
http://img.takeaclick.com/php-my-admin-2.JPG
http://img.takeaclick.com/settings-media.JPG
http://img.takeaclick.com/upload-sub-domain.JPG
Just a heads up I had to make my settings as follow to get it to work.
Store uploads in this folder: img/uploads
Full URL path to files: http://img.domain.com/uploads
Great tutorial and explanation.
I just noticed why is it that your site is still hosting images on the main domain? Just asking, I hope you don't mind.
Hello Pradeep,
Hope all is well,
I would like to know, if i use Next Gen Galleries, how could i change them to sub-domain ?
Kamal.
Yea Kamal, for scripts related to thumbnails, you need to tweak the main plugin files, it's bit complicated as far as I tried. 🙁
Interesting!
How would it work if I point my subdomain to the WordPress default uploads folder?
What values would I need to provide in
Store uploads in this folder :
Full URL path to files :
By the way, what's the difference between the 2 fields?
In "Store uploads in this folder :" mention the folder name
and
In "Full URL path to files :" mention the subdomain URL.
So, for instance:
Store uploads in this folder : /wp-content/uploads
In "Full URL path to files : http://sub.domain.com
That didn't work! It messed up my theme, in fact
I think you didn't check the screenshot mate, here it is :
http://hellboundbloggers.com/wp-content/uploads/2010/06/Subdomain-images.jpg
I said the "folder name", suppose say "Images" or "pics" like that..
Even if the subdomain is pointing to /wp-content/uploads ?
Instead of a new folder like Images or something, my subdomain points to /wp-content/uploads because I don't want to physically move the existing files (and also because I want the installation to be self contained).
Just changed folder to uploads
and path to http://sub.domain.com
This has not messed up my theme….that's encouraging…
I will try to run the SQL query this evening.
Isn't the SQL the other way round?
Instead of
UPDATE wp_posts SET post_content = REPLACE(post_content,’http://img.yourdomain.com/’,'http://www.yourdomain.com/wp-content/uploads/’)
shouldn't it be
UPDATE wp_posts SET post_content = REPLACE(post_content,'http://www.yourdomain.com/wp-content/uploads/’,’http://img.yourdomain.com/’)
Yup that worked. I had to make the above change to the SQL, and also had to replace the quotes with true single quotes.
Thanks, Pradeep.
P.S. As stated above, I set
Store uploads in this folder : uploads
Full URL path to files : http://sub.domain.com
(http://sub.domain.com points to http://www.domain.com/wp-content/uploads)
Thanks Klymene for picking HBB for this tutorial. 🙂
Good tutorial, but my query is whether to use same webhost or use some CDN for hosting the images..
P.S why haven't u implemented this in your blog???
Hi Raj,
We thought of moving all our files to external CDN (like MaxCDN), so we didn't apply this. 🙂
Hi I have already set all the tutorial but when I try to upload a new image will not upload the picture goes blank, that puevo be?
The picture goes blank? Did you try uploading another image?
ya intente subir como 15 imagenes, se crea la dirección de la image pero la image no va en el subdominio y se queda en blanco.
and try to climb as 15 images, establishing the direction of the image but the image is not in the subdomain is blank.
Mind sharing the URL of the image Alyen? 🙂
http://m.tecnux.net/suporte-zynga-informacao-decontato.png
SS:
http://imageshack.us/photo/my-images/51/errorwpn.png/
Full details of My hosting is here..
http://jpss.in/webhosting.php
” Unlimited plan “
As per the details given in that site, you are using Shared Hosting. You can better try a CDN for hosting images. 🙂
I am not using shared hosting & CDN.
So what is your hosting mate?
currently my site is http://www.amigowork.com
now i want to store images on my sub domains like..
img1.amigowork.com
img2.amigowork.com
img3.amigowork.com
Is it possible ?
Are you using shared hosting or any other mate?
Using any other CDN?
Can i use more then one subdomain to store my images ?
if yes.. then explain me how.. ?
Which SQL queries i should use for it ?
More than one subdomain for storing the images from the same blog?!
EXCELLENT
Do this create a problem while taking wordpress backup ?
It should not. But you may face problems while using tinthumb.php scripts.
I’ve been looking to start a new site, but am having a difficulty getting my foot on the ground. Browsing other blogs, I’ve found that a site like Flickr has been known to delete images that’ve you stored on it- if you use it on your own site. Where do you think the best place to host images are? I don’t want to have them on my hard drive and take up the space, so typically I’d prefer an online site/server.
This is a cool trick to host images in subdomain. So, is there a way to host images on another domain? … I know, put in the image URLs MANUALLY…
Hehe… 🙂
thanks ! but when i buy a new host and point it to images.domain! i got an error: you can upload your images – maybe y… something like that ! can any body help ?
Do the same procedure in your new host. 🙂
Thanks for information. one silly doubt, i usually create a subdomain for theme demo and script.
should i create and subdomain with wordpress install,
or
simply create a folder in the “/publichtml/img” -folder in using Filezilla in FTP ?? and change the media link in WP Dashboard.
” i dont want to take sudden risk in my blog “
No buddy, you don’t need to install WP on the subdomain, just create a folder there and link it with the Dashboard thing. 🙂
😎 Thanks for the tip 😆
Can you please help.
I get
#1146 – Table ‘cj******_cj*****.wp_posts’ doesn’t exist
P.s i put the *’s in just in case.
Please Help!
I would love to have a screenshot Chris!! 🙂
Got the screenshot mate. Thanks!
And I think you renamed your wp-posts into something else for security purpose.
Did you follow all the procedures!?
When I visited this image URL I was redirected to this image URL (http://storage.cjs.au.com/images/gadgetbag/media/2010/08/Justin+Bieber+90282309-300×190.jpg)
Did you create separate subdomain or domain for images!? 🙂
this is the subdomain where im keeping the images now.
That’s a new post though so it’s using the new domain, older posts still use the old domain and if they dont look like it, they might have been redirected with the htaccess.
Oh and i thought you had to change wp_posts to your table name because this happens when I use:
UPDATE wp_posts SET post_content = REPLACE(post_content,’http://storage.cjs.au.com/images/gadgetbag/media/’,’http://www.gadgetbag.au.com/wp-content/uploads/’)
Screenshot: http://storage.cjs.au.com/images/gadgetbag/media/2010/08/help2.jpg
Sorry for the confusions and thanks for your help!
have i replied yet?
I think you should check this comment mate,
http://hellboundbloggers.com/2010/06/13/host-images-of-wordpress-blog-in-subdomain/
He has replied,
“Your sql query has the paths reversed. When I first tried it I got “0 rows affected”. When I changed the query to put the path being changed first, then it worked.”
Hi, There is no option page like Miscellaneous under Settings option in WP 3.0 ,How to achieve the above in WP 3.0
Gokul, go to Settings –> Media. 🙂
Hello sir,
In WordPress 3.0 ,there is no option like you said in”settings->Media”,to specify upload location
Sorry Gokul, that was for WordPress 3.0.1. Do you want to try this on previous version of WordPress (3.0)?
thanks for the awesome tutorial.
wow..never tried this things….will try out…i saw orkut also doing the same procedure..!! gr8 info
Not working for me Pradeep. Checked it and tried it a couple of times. But I am missing something miniscule.
I will try this later on again.
I am a new blogger and followed the directions above. I didn’t need to move any old images since I am working on my first post.
This may sound dumb, but where do I find the folder on FTP. There was the original Images folder. Shouldn’t there be a second one somewhere?
A very helpful tutorial. However, I did find some mistakes…
1) To change the upload path, in WordPress 3 at least, you have to go to Settings > Media. There is no Miscellaneous.
2) Your sql query has the paths reversed. When I first tried it I got “0 rows affected”. When I changed the query to put the path being changed first, then it worked.
Glad you found this tutorial useful mate. Yeah, some options have been changed in WP 3.0. 🙂
Thats a very nice and useful info mate,keep up your good work 🙂 🙂
Nice post mate 🙂 Thnx for share
good idea
Nice tutorial. But could you explain how exactly will it speed up your blog’s loading speed? After all, we are storing the images in the same domain name, right?
This is a very nice and useful post for all blogs who like images in post, I have being thinking that I will not be using image in all my post again, but with is powerful tutorial, I think it is a good method to speed up your blog, if you are the type that like image in post as I do.
Hello pradeep 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 pradeep.
I checked your blog. Guess it is fine now.
It’s working. Any issues from your side mate?
Ya I changed all images address to new sub domain but the problem is, when I try to upload new images in post it is not uploading! Have u installed WP on sub-domian?
Not uploading in the sense bro? You get any error?
Plz make sure the values in Miscellaneous are as follows :
Store uploads in this folder : (The name of the folder you have created)
Full URL path to files : (The subdomain you have created for storing the images)
And I didn’t install WP on Subdomain!
Ok, seems it won’t worked for me 🙁 now I revert my all my images back to main domain.
Nice tutorial.Your blog is really loading faster than previous theme.This theme is really looking good and working well for you except comluv 🙂
Hey brother you have changed your theme, the new theme also looking very nice. Yes that is one nice tips for increase our blog loading speed.
That’s a real handy tip man. I never even thought about it yet…
PS: I heard that you suffered an AdSense ban lately.. Is it true?
It’s quite simple and worthy bro!
And yes buddy, my adsense account was banned by Google Inc. 🙁
And I can’t able to get the correct reason from them.. still waiting.. 😀
Improper ad placements? or SPAM clicks? 🙁
I seen many of the bloggers uploading there images to subdomain for speed purpose, I’m also thinking to do so after seeing your post. Thanks for this post.
Amazing Pradeep. This is surely going to be worked out by me. I had been plying around to reduce the loading time of my blog and I suppose this would help me.
I realised that your blog’s loading time has come down drastically after you have changed the blog’s look and feel.
Like your plain theme now. Looks like a news site. What’s the theme?
Thanks for the words mate.
Theme name is Bureau News.. 🙂
Nice tip. thanks a lot Pradeep.
nice post 🙂 buddy