• X

    How to Serve Images from a Sub-Domain [WordPress]

    By in WordPress

    Bluehost hosting

    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.

    subdomain making How to Serve Images from a Sub Domain [WordPress]

    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.

    enter subdomain name 600x113 How to Serve Images from a Sub Domain [WordPress]

    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.

    enter subdomain in wordpress 600x379 How to Serve Images from a Sub Domain [WordPress]

    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.

    php my admin How to Serve Images from a Sub Domain [WordPress]

    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.

    run my sql query 600x379 How to Serve Images from a Sub Domain [WordPress]

    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?

    This is a guest post by Yousuf. If you would like to write for ShoutMeLoud, check our guest posting guidelines.

    Subscribe to Download WordPress Guide Worth $99

    Discover more awesome articles

    Article by

    Yousuf has written 6 articles.

    If you like This post, you can follow ShoutMeLoud on Twitter. Subscribe to Blogging tips via RSS or EMAIL to receive instant updates.

    { 25 comments… read them below or add one }

    Raj

    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..

    Reply

    Dinesh

    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.

    Reply

    Mukesh

    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.

    Reply

    ash_nallawalla

    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?

    Reply

    Deepak Singh

    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?

    Reply

    skyafar

    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?

    Reply

    Alief

    no, its bad idea. don’t serve something from many source even that you own subdomain. just use 1, its better.

    Reply

    Karan Labra

    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.

    Reply

    Isha Singh

    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?

    Reply

    rakesh kumar

    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?

    Reply

    Aakash Rohilla

    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.

    Reply

    Luke

    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.

    Reply

    rasivell

    do this work on new vertion (3.5) ?? or no??

    Reply

    Jit dutta

    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?

    Reply

    Gaurav Pundir

    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

    Reply

    Jit Dutta

    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/

    Reply

    Nikhil

    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.

    Reply

    gaurav

    sir I can find this values please tell me the exact location

    chathu

    After i follow this tutorial, I got 404 error for my sub domain. What could be the reason? Any idea?

    Reply

    Akhil

    An excellent tutorial. You well explained all steps.

    I just implemented on my site as “static.digitalbuzzy.com”

    Thanks a lot.

    Reply

    D

    Thank you for the tutorial but I am trying to follow these steps and getting some very strange errors. Not sure what exactly is going on…

    Reply

    Smantha Webburr

    Thanks of the tutorial but i am having an issue….all work fine for me…

    but when i add redirection in .htaccess file it will create redirecting loop because we already changed in the database and again changing in .htaccess file….please advice??

    Reply

    Smantha Webburr

    Hello..This tricks work for me..but I planned to used a CDN & would like my images back on the original path wp-content/upload…..i had done that by the sql query & deleting everything from Wp-Original-media-path plugin….

    Now Problem is that google has crawl all images from my sub domain and indexed them.. also… as jit dutta told the above redirection in .htaccess file i used them & its working….

    Now can you tell me .htaccess redirection from sub-domain to original path to avoid duplicate content in search engine..

    Reply

    Rakesh

    Harsh, I think this is now a feature that is unavailable in wordpress latest versions. I do not find the media file path option and nothing like the screenshot proves to be working with latest versions. Waiting for your opinion and the real facts about this option ,

    Reply

    Walter Pinem

    nice tutorials, but it doesnt work at all for me.
    The images on my main domain are not available, even when I’m done in moving it to my subdomain.
    What am I supposed to do?
    HELP!

    Reply

    Leave a Comment

    Previous post:

    Next post:

    `