• Skip to main content
  • Skip to primary sidebar
  • About
  • Contact
  • Resources
  • Blog
    • Easy Digital Downloads
    • General
    • Genesis Framework
    • jQuery/JavaScript
    • Project Management
    • WordPress Functions
    • WordPress Plugin Development
  • Army Lessons

Scott DeLuzio

WordPress Development Tutorials

Serve Images From Sub-Domain in WordPress

June 20, 2014 Scott DeLuzio 82 Comments

Anyone who is interested in speeding up their website’s load time has probably run their site through a tool like Pingdom’s Website Speed Test, GTmetrix, or YSlow. While testing your site, you likely have received some sort of message indicating that you should serve static content from sub-domain or a domain that doesn’t serve cookies. This doesn’t mean that you have to go out and purchase another domain name to serve images and other static content. Instead, you can use your existing website to serve images from sub-domain. This tutorial will show you how.

Why Serve Static Content From a Sub-Domain?

Serving static content like images from a sub-domain has a few benefits.

  1. If the sub-domain doesn’t set cookies, a cookie won’t get sent to the browser for each request made to the files on the sub-domain. Cookies are typically very small, so they don’t add a lot of “weight” to your page. They are still unnecessary for static content, and getting rid of all the excess “bulk” to your site is important if you are interested in a fast loading site (you should be).
  2. Modern web browsers can only download a certain number of resources from a domain at once. By creating a sub-domain to serve resources from, you are effectively doubling the number of resources that a visitor’s browser can download at once – or in parallel. For argument’s sake, this means that a web browser that can download two resources at a time will visit a page with 10 resources (images, CSS, HTML, etc.) and have to wait to download the first two before it can start to download the second two. The same goes for the third, fourth, and fifth set of resources. If the site is set up to serve static content from a sub-domain, the browser can download four resources at once by downloading two from the main domain, and two from the sub-domain.

How to Set WordPress to Serve Images From Sub-Domain

Important: Before continuing with this tutorial, please make a complete backup of your website and database. You will be instructed to run updates on your database and if something should go wrong you will need the backup to fall back on. You have been warned!

Setting up WordPress to serve images from sub-domain requires that you

  • Create a sub-domain
  • Tell WordPress where to upload images to
  • Update existing images in posts to point to the sub-domain for images
  • [Optional] Redirect old image references to the sub-domain

Creating a Sub-Domain

Log in to your website’s cPanel and scroll down to the Domains menu. Find Subdomains and click the link.

Sub-domain cPanel

Next enter a name for the sub-domain you are creating. This can be whatever you want it to be. I’m choosing img but other sites use static, images, content, etc. It really doesn’t matter though.

What does matter is what you enter into the Document Root box. By default, this will grab the sub-domain you entered and add it to public_html like this: public_html/img. This is not what you want.

Instead, delete the img part (or whatever you used for your sub-domain) and replace it with wp-content/uploads. Then click Create.

Create Subdomain

So far all we have done is created a sub-domain that uses your wp-content/uploads folder as a document root. This basically means that a URL pointing to http://yoursite.com/wp-content/uploads/2014/06/image.png is pointing to the same place on your server as http://img.yoursite.com/2014/06/image.png now points to.

Tell WordPress Where To Upload Images

If we don’t tell WordPress that your sub-domain is the new URL to upload images to, all your images will continue to upload to http://yoursite.com/wp-content/uploads/. Actually, even if we tell WordPress to upload images to your sub-domain, your images will still end up in the /wp-content/uploads folder.

The reason why we need to tell WordPress to use the sub-domain URL is so that when you insert an image into a page or post you won’t continue to use the http://yoursite.com/wp-content/uploads/ URL. Remember, we’re trying to get the visitor’s browser to use the sub-domain to access static content, so we need to make sure each image uses the sub-domain URL to display images.

In earlier versions of WordPress, there was an option in the Media Settings, which later was moved to Miscellaneous Settings that allowed you to set the full URL path to files. In an effort to clean up the user interface this option has been “hidden”, but it is still available.

There is a Hidden Admin Options screen that you can access by going to http://yoursite.com/wp-admin/options.php. Once there, search for the option labeled upload_url_path. Enter your full sub-domain without the trailing slash. Scroll to the bottom of the page and click on the Save Changes button.

Upload URL Path

If this is a new site with no images uploaded to the site yet, pat yourself on the back – you’re done!

If you are adding a sub-domain to an existing site, then you will likely want to update the images that already are in your pages and posts so that they use the sub-domain URL path as well.

Update Existing Images to Point to Sub-Domain

Log back in to your website’s cPanel and scroll down to the Databases section, and click on phpMyAdmin.

Navigate to your WordPress site’s database if you have more than one database set up. Click on the SQL tab in the navigation bar towards the top of the screen.

Update Serve Images From Sub-Domain

Enter the following code in the query box:

UPDATE wp_posts SET post_content = REPLACE (post_content,'http://yoursite.com/wp-content/uploads/','http://img.yoursite.com/');

Click the Go button below that box. Once it is done, you should receive a success message at the top of the screen with a green background indicating XX number of rows affected.

Important: Change http://yoursite.com to the location of your WordPress site!

What this does is takes the source URLs from all the images in your existing posts, and it changes them so that they point to the new sub-domain.

Now go back to your site and check to see that all the images are loading correctly. If not, you may need to revert to the backup of your site that you made before you started. (You did make a backup, right?)

Optional – Redirect Image URLs to Sub-Domain

Technically none of your images have moved on your server. You should still be able to access all your images using either http://yoursite.com/wp-content/uploads/2014/06/image.png or http://img.yoursite.com/2014/06/image.png. That means that any other websites or search engines that were linking to the old URL will still show the image, but won’t be showing it from your sub-domain. It’s not a big deal, but if you care about making sure all requests for those images are pointing to the sub-domain, you should use a redirect.

Open up your .htaccess file, which should be located under http://yoursite.com/.htaccess.

Add the following code to the .htaccess file and save it back on your server:

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

Again, make sure to change http://img.yoursite.com to your actual sub-domain.

Content Delivery Network Issues

If you use a content delivery network (CDN) like Cloudflare or MaxCDN, you may run into a DNS error. Fixing this is relatively simple.

Log in to your CDN account, and navigate to your DNS settings. Each CDN will have this in a different location, so check with them if you don’t know where to find these settings.

Add an A name record that points http://img.yoursite.com to the IP address of your site. In your DNS settings, there will probably already be an A name record for your main domain that has the correct IP address listed. You can probably copy that IP to get the new A name record set up.

WordPress Plugin Development Development, Image, MySQL, SEO

Reader Interactions

Comments

  1. Z says

    October 30, 2014 at 10:40 am

    Hi there. Firstly, great article. It was clear, concise and easy to read. However, I’m still confused and hope you can help.

    1. If the subdomain is installed using same server/ip address as the primary domain, will I still see the increase in speed?

    2. Above in the article you mentioned 2 benefits. The first regarding cookies and the second regarding browser requests. So technically, if I use a cookieless subdomain to serve images, I will receive both benefits. However, what if my subdomain is not cookieless? Am I correct to say that if that’s the case, I will only receive benefit number 2 but not receive benefit number 1?

    Reason I’m asking is because I read that if you use Adsense, you must use a new domain to serve images…subdomains won’t work in this case because of Adsense’s cookie policies.

    Was therefore wondering…if I still insist on using a subdomain that is not cookieless, so that I may use Adsense, is there still a point in using the subdomain (that is not cookieless)? If I still receive benefit number 2, then I’ll go for it.

    Reply
    • Scott says

      October 30, 2014 at 11:28 am

      Hi Z,
      In response to your first point, yes you will see an increase in speed even if the sub-domain is on the same server/IP address as the primary domain. See point #2 under Why Serve Static Content From a Sub-Domain? Basically, browsers will see img.example.com and example.com as two different domains that they can download from. This effectively doubles the number of resources a browser can download at once – it doesn’t double the speed, just the quantity of items (images, CSS files, etc.) that can be downloaded at the same time. Depending on the browser, most modern browsers allow 4-6 connections per domain. By using a sub-domain the number of connections is now between 8-12. More items being downloaded at the same time usually results in a faster page load, as the browser doesn’t have to wait for other items to finish downloading before starting to download more.

      Addressing your second point, yes I believe you are correct. If your sub-domain sets cookies a browser has to download that cookie in addition to all the images and other items coming from that sub-domain. With that said, even though the browser has to now download a cookie from the sub-domain it still benefits from being able to download more items at the same time than it did before. One of those items just happens to be a cookie that you don’t necessarily need.

      As for AdSense, is this the cookie policy you were referring to? If so, this relates to Google’s placement of cookies and has nothing to do with whether or not your domain(s) can set cookies. If you have a standard WordPress install (which is what this tutorial is geared towards), you would insert your AdSense code into a page/post/widget. The AdSense code would then be what handles the placement of a cookie, not your website.

      In that case I believe (although haven’t verified) that the cookie(s), and any images coming from an AdSense ad would be coming from a third domain (#1 – yoursite.com, #2 – img.yoursite.com, #3 – AdSense/Google although I’m not sure what the exact domain is that this would come from). If I am correct, then a visitor’s browser would be able to download 4-6 items from yoursite.com, 4-6 items from img.yoursite.com, and 4-6 items from AdSense all at the same time.

      Also note that the browser will only download 4-6 items from a domain as long as there are items to be downloaded from that domain. For example, if your browser can download 6 items per domain at once and your site has 12 images served from your sub-domain, and 24 other resources from your main domain (total 36 items) your site would then load as follows:

      • 6 images from img.yoursite.com and 6 items from yoursite.com (12/36 items downloaded at this point)
      • 6 images from img.yoursite.com and 6 more items from yoursite.com (24/36 items downloaded at this point)
      • 6 more items from yoursite.com (30/36 items downloaded at this point)
      • 6 more items from yoursite.com (36/36 items downloaded at this point)

      Without a sub-domain, all the images and other items being served from yoursite.com, your browser would download like this:

      • 6 items from yoursite.com (6/36 items downloaded at this point)
      • 6 items from yoursite.com (12/36 items downloaded at this point)
      • 6 items from yoursite.com (18/36 items downloaded at this point)
      • 6 items from yoursite.com (24/36 items downloaded at this point)
      • 6 items from yoursite.com (30/36 items downloaded at this point)
      • 6 items from yoursite.com (36/36 items downloaded at this point)
      Reply
  2. Z says

    October 30, 2014 at 11:05 am

    *Edit

    Additional question regarding my question #1 above.. if subdomain is using same ip/server as primary domain..i will at least receive benefit #2 stated in your article above?

    sorry if you don’t get me, english is not my first language and sorry for troubling!

    Reply
  3. Z says

    October 30, 2014 at 1:34 pm

    Dear Scott, thanks very much for your detailed answer. I have a much greater understanding now.

    Regarding Adsense and subdomains, I think I was mistaken. I read over here http://www.symkat.com/cookieless-domains that:

    “If your website uses the root domain (example.com as opposed to http://www.example.com ,for instance) to serve traffic and uses Google AdSense, you cannot make a cookieless domain as a sub-domain and MUST use an alternative domain name. The reason for this is that the Google AdSense code sets cookies on the requested domain and all sub-domains. They do not allow you to override this behavior.”

    I now realize it says that subdomain + Adsense will not work only if site’s canonical url is example.com

    So, as long as canonical url is http://www.example.com, this means that Adsense + img.example.com should work too?

    Reply
    • Scott says

      October 30, 2014 at 1:56 pm

      Z,
      I honestly don’t know how AdSense would work with regards to the article you referenced. My confusion with the article you mentioned is that they say…

      The reason for this is that the Google AdSense code sets cookies on the requested domain and all sub-domains

      …but that would mean regardless of whether you are using www or not in the URL, AdSense will set a cookie on all sub-domains. Unless they mean all sub-domains of the http://www.example.com (i.e. img.www.example.com)?

      Again, I am not sure how AdSense places cookies on domains/sub-domains, so I can’t say for certain if this is correct or not.

      Reply
      • Z says

        October 30, 2014 at 2:19 pm

        Maybe it means that the www is a subdomain of example.com
        Same problem this person had with subdomain, cookies and adsense lol

        Btw thanks for your help!

        Reply
        • Scott says

          October 30, 2014 at 2:27 pm

          Yes, www in http://www.example.com is a sub-domain of example.com. I think based on the articles you are referencing (assuming they are correct), your best bet is to use www in your URLs, and set the sub-domain of img (or whatever you choose) for your static content.

          Reply
  4. Sayzar Rahman Akash says

    November 28, 2014 at 6:30 am

    Brother,
    Thanks a lot!!! Thanks for this great post! It’s working in my website very nicely! You are Great! GO Ahead!

    Thanks,
    Sayzar Rahman

    Reply
    • Scott says

      December 1, 2014 at 10:04 am

      I’m glad this helped!

      Reply
  5. David Coleman says

    December 9, 2014 at 7:18 am

    Hi Scott,

    Great article—very much appreciated!

    I have one question that I am hoping you can help me with. In the “Optional Redirect” section, you say that “Technically none of your images have moved on your server”.

    My question is this: After making the redirect, should I move all my pre-existing image files to my new sub-domain? That would be preferable, correct?

    Thanks again so much for the helpful post!

    All the best,

    David Coleman

    Reply
    • David Coleman says

      December 9, 2014 at 7:26 am

      Sorry, I forgot to also ask:

      Can I also create subdirectories within my sub-domain for different images? And if I did so, would I have to do anything else for WordPress to recognize images from those different subdirectories?

      For example, Google says at https://support.google.com/webmasters/answer/114016?hl=en :

      Consider structuring your directories so that similar images are saved together. For example, you might have one directory for thumbnails and another for full-size images; or you could create separate directories for each category of images (for example, you could create separate directories for Hawaii, Ghana, and Ireland under your Travel directory).

      Thanks again Scott!

      Reply
      • Scott says

        December 9, 2014 at 2:42 pm

        David,
        Again, see my previous comment about images in the sub-domain. All your images should still be in the regular WordPress /wp-content/uploads folder.
        I don’t know how much benefit you will get from organizing your images in directories for each category of image you have. I believe (although I could be wrong), that Google will not necessarily know that an image exists just because it finds the folder that the image is in. For example, if Google knows that an image is located at yoursite.com/wp-content/uploads/2014/12/image.jpg, it won’t immediately know that there is another image located at yoursite.com/wp-content/uploads/2014/12/photo.jpg. So in their example, just because you organized all your Hawaii photos in a Hawaii directory, it doesn’t mean that they will immediately find all of them just for the fact that they are in the same directory.
        To make sure that Google knows about all your images, you might consider using a plugin such as SEO by Yoast, which allows you to include media in your XML sitemaps. Google webmaster tools lets you submit sitemaps, which tells them where all your site’s content is. Including media in your sitemap would solve this issue.
        If you really want to manipulate the image directories as they suggest, you can try Media File Manager plugin or Media File Manager Advanced, which I believe will do what you want it to. This would be easier than manually moving images every time you upload them into their respective directories.
        Note: I haven’t used either of those media file manager plugins, so I can’t guarantee any results. Backup everything on your site before making any drastic changes like this.

        Reply
    • Scott says

      December 9, 2014 at 2:27 pm

      Hi David,
      Actually, if you had followed all the steps in this tutorial, you won’t be able to move the pre-existing images into the sub-domain and have this work.
      Here’s why.
      In the example, you created the subdomain img.yoursite.com, which uses /wp-content/uploads as the document root. Basically this means that entering the URL img.yoursite.com and yoursite.com/wp-content/uploads are two ways of entering a URL to the same location.
      So…
      img.yoursite.com/2014/12/image.png
      is the same as
      yoursite.com/wp-content/uploads/2014/12/image.png

      Additionally…
      img.yoursite.com/2014/12/photo.png
      is not the same as
      yoursite.com/img/2014/12/photo.png (which I’m assuming is where you were asking if you could move your existing images to).

      That final step for redirecting image URLs to the sub-domain really is optional, and honestly I don’t think it will have any impact on your site’s performance whether you do it or not. If anything it may slightly slow down the requests coming from other sites for your site’s media as the old URLs (yoursite.com/wp-content/uploads/2014/12/image.jpg) will be redirected to the sub-domain (img.yoursite.com/2014/12/image.jpg). I only included that step to show how it might be done if anyone wanted to do it. It certainly isn’t necessary though, and images from your site using the old URLs will continue to display just fine.

      Reply
      • David Coleman says

        December 9, 2014 at 4:06 pm

        Scott,

        Thank you so much for the reply, you were a lot of help! Also, thank you for the links to the plugins, I’ll definitely look into them.

        All the best,

        David

        Reply
        • David Coleman says

          December 9, 2014 at 6:10 pm

          Hi Scott,

          I tried to get this going, but I’ve ran into a snag. When I try to point to my sub-domain in phpMyAdmin, I get this warning after I run the code in the query box:

          Table ‘insdebz_db5697.wp_posts’ doesn’t exist

          I have my WordPress database selected, and have changed the code to the appropriate URLs for my site. I’m 99% sure I’ve done everything else correctly up to this point.

          Any ideas? I don’t want to take up your time, but thought I’d ask in case there might be an obvious issue I’m missing.

          Thanks Scott.

          David

          Reply
  6. David Coleman says

    December 9, 2014 at 6:49 pm

    Scott,

    I noticed the beginning of the code shows “wp_posts” like so:

    UPDATE wp_posts SET post_content = REPLACE …

    I have a custom prefix on my database (not wp_). Do I need to make that change in the code above (add my custom prefix)?

    Reply
  7. Gulshan Kumar says

    January 3, 2015 at 11:58 am

    When I try to create subdomain. It set automatically directory as:-

    img.example.com /home/uxxxxxx/public_html/img

    How can I solve this problem?

    Reply
    • Scott says

      January 5, 2015 at 10:55 am

      See this screenshot (also found under the Creating a Sub-Domain section above), which indicates that you need to change the directory to point to your WordPress uploads directory.
      By default it will attempt to create the directory as /home/xxxxxx/public_html/img. You need to manually remove the img part of that, and make it wp-content/uploads.
      Hope this helps.

      Reply
  8. Rajan says

    January 29, 2015 at 12:21 pm

    Hi Scott,
    First of all, thanks a lot for your explanation. This process was listed on various websites but only you were able to explain the logic behind it the best.
    I had carefully done all the steps on my blog http://techrace.in including editing the .htaccess file but still the images don’t load from subdomain.
    Can you please help with me with this?

    Reply
    • Scott says

      January 30, 2015 at 11:36 am

      Hi Rajan,
      Thanks for the kind words.
      As far as I can tell you have set up the sub-domain correctly. See this image on your site: http://img.techrace.in/2015/01/lumia33-600×482-150×150.jpg, which you can tell is pointing to the sub-domain.
      Now the step you might be missing is editing the existing image src to point the images to your sub-domain. Did you follow the steps in the “Update Existing Images to Point to Sub-Domain” section above? If you did, and it still isn’t working, then your theme might be forcing the images to be called from the regular wp-content/uploads directory.
      Hope this helps!

      Reply
  9. Martin says

    February 8, 2015 at 6:56 am

    Great article. Quick question how do you deal with the subdomain in your development environment? Or do you just change the image urls once you are live?

    Reply
    • Scott says

      February 9, 2015 at 9:36 am

      In a development environment, I’m usually less concerned with this aspect as I am getting the site ready to go live. I usually wouldn’t mess with the sub-domain in a development environment because I’d have to update the URLs again once the site went live anyways (i.e. img.testsite.devserver.com to img.testsite.com). However, as I’m writing this I started thinking of something I hadn’t considered before.
      If I’m looking to have the sub-domain set up on the development environment, you may be able to set up the sub-domain on the live site and upload all the images there. Then on the development site, you could make sure the img src points to the sub-domain on the live server. This way images would be pulling from the live server when being viewed on the dev server. When the site eventually gets migrated over to the live server, no changes would need to be made in order for the sub-domain to continue working properly on those images. Going forward, you’d just need to add the sub-domain to the upload_url_path on the live site for any future images.
      I haven’t tested this out yet, but I would assume that it would work OK.

      Reply
  10. Lalit says

    March 20, 2015 at 1:57 pm

    Dude, you are genius to put subdomain Path as upload folder. i used to copy all files to a new folder which used to be root of the subdomain, so inefficient it was.

    also your website fonts are so big. i had to scroll a lot . thinking to fix them anytime sooner?

    Reply
  11. Yohan Perera says

    June 23, 2015 at 10:14 pm

    Hi Scott,

    Loved this article. Especially the instructions about changing the CDN settings. (I am on MaxCDN). One question though. Will this result in redundant files? In other words will the old files reside in the default location and the new sub domain also?

    Reply
    • Ghulam Yasin says

      September 8, 2015 at 6:47 pm

      Hi scott,
      Yes old files will remain in default location and new files are also being uploaded to the same default location. Because you have used (public_html/wp-content/uploads) as Document root for your subdomain.

      Reply
  12. Aasiyah says

    September 29, 2015 at 8:12 am

    Hi there. Great article. It was clear, concise and easy to read.
    I got it right the first time unberlievable.Thanks so much for making it possible,
    site is loading way much faster.
    Thanks again

    Reply
  13. AdeHaze says

    December 19, 2015 at 5:27 pm

    I’ve been all night looking for tutorials, finally finished. thank you

    Reply
  14. Jason Jay says

    January 25, 2016 at 8:50 am

    Hey Scott, Thank you for posting this help, im a WordPress newbie and your tutorial was easy to follow and really helpful..

    my site is now completely broken and i want to sue you , no only joking,

    i have set up my site to Serve the Images From Sub-Domain successfully (yeah!!)

    but now i want to rename my WP-content folder for better security. would that cause a problem?

    cheers Jason

    Reply
    • Scott says

      January 25, 2016 at 9:25 am

      Hi Jason,
      You can rename the wp-content folder, but it isn’t as simple as changing the name of the folder and having everything work for you. WordPress sort of expects the wp-content folder to be there unless it is told otherwise. To do that, you’ll need to add a bit of code to the wp-config.php file. You should also make a full backup of everything (files, database, etc.) before you get started.

      First, change the name of wp-content folder to whatever you want it to be (this example uses “assets”). Then edit wp-config.php and add the following lines just before the /* That's all, stop editing! Happy blogging. */

      define ('WP_CONTENT_FOLDERNAME', 'assets'); // Change assets to whatever you renamed wp-content to.
      define ('WP_CONTENT_DIR', ABSPATH . WP_CONTENT_FOLDERNAME);
      define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/'); // If you installed WordPress in another directory (i.e. http://yoursite.com/blog), you'll need to add the directory name after the last '/' (i.e. '/blog/'). Also if you're using https, or www in the URL you'll need to change that as well (i.e. 'https://www.').
      define('WP_CONTENT_URL', WP_SITEURL . WP_CONTENT_FOLDERNAME);

      This site has a good write up on how this works.

      While this should work, you should realize that not all themes and plugins are coded to work with a different directory name (some have wp-content hard coded in their code). Also, if you have any existing images or other media in your site already you’ll need to update their source to point to your new folder instead of wp-content.

      Reply
  15. khan says

    April 1, 2016 at 8:27 pm

    Hi. i am using wordpress 4.4.2
    i dont have permission to edit options.php file ?

    Reply
    • Scott says

      April 4, 2016 at 2:23 pm

      You may not be able to edit the site options if you are not logged in with an administrator level account.

      Reply
  16. Robertino Bezman says

    April 15, 2016 at 6:35 am

    Hi, Scott.

    Great article. Short but very helpful.

    Still, something seems not working for me. I have WP 4.5. I managed to create the sub-domain and made the code change in my database. But when I try to add the full sub-domain in “upload_url_path” after I press SAVE CHANGES, the field is… empty. Any ideea?

    Reply
    • Robertino Bezman says

      April 15, 2016 at 6:43 am

      Sorry, I forget the link to my site http://www.robifoto.ro.

      Reply
    • Scott says

      April 15, 2016 at 8:53 am

      Is your site on a multisite by any chance? I believe the option to edit this setting is disabled on multisite.

      Reply
      • Robertino Bezman says

        April 15, 2016 at 11:13 am

        Well, no, it is not on a multisite.

        Reply
        • Robertino Bezman says

          April 15, 2016 at 11:55 am

          And if it helps: after I add the code in the .httacces, the site loads quicker but the images are not displayed anymore.

          Reply
          • Scott says

            April 15, 2016 at 12:17 pm

            What is your site URL?

  17. Robertino Bezman says

    April 15, 2016 at 12:22 pm

    I already wrote it in a comment above http://www.robifoto.ro.

    Reply
    • Scott says

      April 15, 2016 at 12:38 pm

      Sorry, I meant the subdomain that you set up. I wasn’t very clear.

      Reply
  18. Robertino Bezman says

    April 15, 2016 at 2:51 pm

    Ok, no problem. This is it: http://img.robifoto.ro

    Reply
    • Scott says

      April 15, 2016 at 3:17 pm

      You might need to set your site’s DNS records to point the subdomain to the IP of your server. See the DNS propagation of https://www.whatsmydns.net/#A/robifoto.ro
      and https://www.whatsmydns.net/#A/img.robifoto.ro
      As for why the site isn’t allowing you to save the changes to the upload_url_path, I’m not entirely sure what the problem would be.
      You can try adding define(‘WP_CONTENT_URL’, ‘http://img.robifoto.ro/’); to your wp-config.php just before the /* That’s all, stop editing! Happy blogging. */ line. I haven’t tested this, but I believe it would accomplish the same thing.

      Reply
  19. Stefan says

    November 21, 2016 at 3:34 am

    Hi, nice tut!
    I want to do this but I have VPS Debian and this tutorial not working for me. Also, I have SSL and that is biggest problem. Somehow I success, images in articles works, but not thumbnails (featured images, because their image path are defined in CSS as background img). URL is ok everywhere (img.domain.com) but they have SSL for featured images only (in CSS), and I don’t have SSL for sub-domain so images not appear. Can you make tutorial how to setup all this on Debian VPS Apache with SSL? Thanks!

    Reply
    • Scott says

      November 21, 2016 at 11:58 am

      You would need an SSL certificate on your subdomain for this to work correctly. Really that’s the only difference. You probably aren’t seeing the images because it is trying to display insecure content on a secure page.

      Reply
      • Stefan says

        November 22, 2016 at 4:41 am

        Thank you for replay. So, I can’t do this until I pay another SSL? (for subdomain).

        Reply
        • Scott says

          November 23, 2016 at 1:59 pm

          Based on my understanding of your setup, yes that is correct. If the subdomain is secured with a certificate, you should be able to do everything as described in the article. You may be able to get a wildcard SSL certificate that covers the main domain and any subdomains, or use a free SSL provider like Let’s Encrypt if you don’t want to double the SSL certificate payment.

          Reply
  20. Yatish says

    January 5, 2017 at 12:08 am

    Hey Scott,
    Really nice tutorial. Well done! Very easy read …

    So i would like to try this out on a test site for now to see if we can implement on the site as the solution and before going live. Few things if you don’t mind assisting or advising on please … (i replaced the site url name to ‘site’ for now for privacy purposes)

    1. How can i add the subdomain under a Ubuntu Apache PHP7 environment? Got the virtual host done with the following:

    …
    ServerName site.com
    ServerAlias site.com *.site.com http://www.site.com
    DocumentRoot /var/www/html
    …

    Do i create it as a new virtual host site or just as a virtual host under this one?

    2. It is a Multisite site so i can access http://www.site.com/wp-admin/options.php and http://subsite.site.com/wp-admin/options.php

    3. Do you think this could be an option as a central media library for all network sites? instead of a media library per site on the network?

    Would be great to have your feedback.

    Thank in advance
    Yatish

    Reply
    • Scott says

      January 5, 2017 at 4:49 pm

      Hi Yatish,
      To be honest, I haven’t tried this in the server environment you are working with. I would imagine that the concept is the same, but likely the “how-to” is different. I’m afraid I wouldn’t be much help there.

      I don’t know how your multisite will be set up, but I would caution against creating a central media library for all of the network sites. Think about if (when) you decide to remove one or more of those sites. Their images will probably remain on the “central” media library, just taking up space. I believe that the multisite setup would be able to handle the cleanup when images are in the appropriate /uploads/site/xx/ directory, but probably not if they are all lumped into one directory.

      Reply
  21. ankit singh says

    June 7, 2017 at 9:15 am

    First of all thank for your great article, I have ask certificate to main domain but I don’t have ssl certificate to sub domain.

    Now
    I have created to sub domain
    1 For uploads
    2 for wp-content

    1. Cdn.scoopfeed.me
    2. Static.scoopfeed.me

    I don’t have ssl on this two sub domain

    Document roots
    1. Public_html/wp-content/uploads/
    2. Public_html/wp-content/

    Now I tried with 1st
    Cdn.scoopfeed.me and has update the wp_posts even added rewrite rules

    But it doesn’t work and how can I change old upload post media url

    Reply
    • Scott DeLuzio says

      June 7, 2017 at 10:18 am

      For this to work correctly without showing insecure content warnings, you would need an SSL certificate for the main domain and the subdomains. You can get a free certificate for both using Let’s Encrypt if your host offers it. If not, there are many affordable hosts that do offer it that you can transfer to.

      Reply
  22. wrestling culture says

    July 30, 2017 at 12:01 am

    Hi Scott,
    Very nice post I am trying and it’s works,
    Thank you so much for sharing
    God bless you ,you solved my problem
    Thank you

    Reply
  23. Ritvik Kulkarni says

    August 12, 2017 at 4:36 am

    Hello,
    Just one problem on an otherwise great article. I did everything exactly as mentioned in this article but now i don’t seem to be able to upload any images using the wordpress uploader. Any reason for this? It just says could not move the image to folder.

    Thank you

    Reply
    • Scott DeLuzio says

      August 14, 2017 at 8:24 am

      Hi,
      Are you sure you have the settings discussed in the section titled TELL WORDPRESS WHERE TO UPLOAD IMAGES correct?

      Reply
  24. Anu says

    August 22, 2017 at 6:49 pm

    Hi Scott, thanks for this useful article. I had one question is there any option to change the old thumbnails in bulk in the /wp-admin/option.php. I had around 500 thumb nails already present in it changing one by one will be too lengthy. Please suggest.

    Reply
    • Scott DeLuzio says

      August 23, 2017 at 8:50 am

      Using a plugin like Better Search Replace (https://wordpress.org/plugins/better-search-replace/) can do this for you. Using that plugin is basically the same idea as the steps I outlined in the “UPDATE EXISTING IMAGES TO POINT TO SUB-DOMAIN” section, except it scans through the entire database.

      Reply
  25. Criss says

    October 6, 2017 at 8:40 am

    There’s something that prevents me from doing all above. You insist on creating /wp-content/uploads in the subdomain. But that is only for images. What about CSS and JS, which can also be outside that folder? Wouldn’t the main site not be able to deliver static content if they are ouside /uploads? So, what happens if I just create subdomain and point it to /public_html ?

    Reply
  26. Milind Padhye says

    January 26, 2018 at 2:05 pm

    Excellent article and simple English that I love.
    One query I have is, can’t something be done for hosting other static files like CSS or JS files? I know that images are added all the time and their number grows. I feel not all bloggers can afford the CDN charges when using a simple sub-domain CDN can do the trick.
    Please keep up with your good work.

    Reply
    • Scott DeLuzio says

      January 31, 2018 at 9:57 am

      There are freely available plugins that can combine multiple CSS and JS files into one, which can drastically reduce the number of files being loaded at once. While that may not give you a 100% perfect score on that mark, it should improve it to an acceptable level enough so that this “trick” isn’t really necessary.

      Reply
  27. Phil Cheney says

    March 28, 2018 at 7:44 am

    Hi
    I have just completed your suggested updates to use a subdomain.
    It was the clearest I found, thank you!
    Phil

    Reply
  28. Steve says

    April 2, 2018 at 9:17 pm

    Hi Scott,

    Thanks for the very helpful post.

    Can you tell me how I stop access to the new sub-domain home page please?

    Obviously, we don’t want to allow access to this page because it’s only for images etc so we need to redirect requests for http://img.example.com (with or without a trailing slash or prefixed www) to the main site homepage but I can’t get it to work!

    Thanks again.

    Reply
  29. Sergio Leone says

    May 21, 2018 at 3:50 am

    Static content serving subdomain configuration explained in a clear and concise manner, simply exactly what I was looking for. Thanks Scott!

    Reply
  30. Dr.Midas says

    May 27, 2018 at 4:47 pm

    Thanks for the article, it was great to read. but it didn’t work for me. Whatever I do, I couldn’t change the upload path.
    First I created the subdomain, ‘img.carsoncharge.com’
    then moved the files to that folder.
    To change the default upload folder, I edited the options.php and defined the subdomain folder but it didn’t work, I used a plugin to change it, it didn’t work, I added the code snipped to config.php, (define UPLOAD…. etc… ) but it didn’t work. no matter what I did, it keeps uploading images to the same folder. What can I do to go around this problem?
    I have this code as a solution for parallelize downloads across hostnames;
    function parallelize_hostnames($url, $id) {
    $hostname = par_get_hostname($url); //call supplemental function
    $url = str_replace(parse_url(get_bloginfo(‘url’), PHP_URL_HOST), $hostname, $url);
    return $url;
    }
    function par_get_hostname($name) {
    $subdomains = array(‘img.carsoncharge.com’); //add your subdomains here, as many as you want.
    $host = abs(crc32(basename($name)) % count($subdomains));
    $hostname = $subdomains[$host];
    return $hostname;
    }
    add_filter(‘wp_get_attachment_url’, ‘parallelize_hostnames’, 10, 2);

    and when I define the upload path from options.php and upload a file, the image url becomes something like that : https://img.img.carsoncharge.com/wp-content/uploads/2018/05/a-10.jpg
    It doubles the ‘img’. I cant somve the problem no matter what I do.

    Reply
  31. Alex says

    August 17, 2018 at 7:11 am

    Hi Scott,

    Thanks for this great article. I follow all your steps above, but for some reason images are not displaying when I add them to a blog post or page.

    However when I go to the image URL on the subdomain it does display correctly.

    Also when I view the code of the blog post it also contains the correct URL, but on the front end it does not display the images.

    Do you know why this is?

    Reply
  32. Fajar Adhietya says

    December 17, 2018 at 6:15 pm

    Hi Scott, i have followed all the instructions that you gave and everything’s run well. But the problem is when I do fetch and render my site in webmasters, Google can’t render my image, maybe because the image located in subdomain (subdomain is different property in webmaster) . Isn’t bad for SEO? Please read in this article https://support.google.com/webmasters/answer/6066468?hl=en (read at line “ressource patch error”. Hopefully you can explained this issue. Thanks

    Reply
  33. Nora says

    December 21, 2018 at 11:36 pm

    Hi. Thanks for sharing.
    I followed the steps but my images URLs show 500 “Internal Server Error.”
    And I go to my sub-domain, it shows 403 error.

    I asked my server support team and he said …
    —————————-
    The subdomain img.mydomain.com now points to this folder public_html/wp-content/uploads. This folder, however, does have a lot of different subfolders within and subfolders within the first subfolders and then you will see the images, which is why if you go to >> img.mydomain.com it loads as 403, because it has no files.
    —————————-

    He said he could not help me on this issue, so suggested me contact people who have succeeded. Could you help me here? I’m really annoyed cus I really do not know how to fix this. Thank you.

    Reply
  34. wwe raw 2018new says

    January 11, 2019 at 12:06 pm

    Static content serving subdomain configuration explained in a clear and concise manner, simply exactly what I was looking for. Thanks Scott!

    Reply
  35. Mark says

    March 6, 2019 at 12:13 pm

    I love the clarity of your article. Unfortunately, I am unable to save the subdomain URL in the upload_url_path. At first I was getting a 403 Forbidden error, but got that resolved by changing permissions. Now it simply won’t save the URL when the Save changes button is clicked.

    Any ideas?

    Reply
  36. Robert Cristian says

    March 28, 2019 at 4:40 am

    if someone accesses the domain http://img.yoursite.com they will see all the photos how i hide this ?

    Reply
    • Scott DeLuzio says

      April 2, 2019 at 9:44 am

      I think that is sort of the point, isn’t it? Prior to changing the URL, the photos would be accessible from http://yoursite.com wouldn’t they? That’s how the page is able to render the photos in the first place and as a consequence if someone types in the direct URL to the image they will be able to see the photo. It’s the same thing as when you are pointing the images to a subdomain. There will always be a way for someone to view the photos no matter what URL structure you’re using, unless you completely disable access to the photos, but then they wouldn’t show up in the posts or pages you’ve inserted them into in the first place.

      Unless I’m misunderstanding the question, I don’t think what you’re asking for is possible.

      Reply
  37. George says

    May 17, 2019 at 5:27 am

    Hi Scott,

    I truly believe that your article is one of the best someone can find for this topic! Congrats!

    Do you think SEO score will be affected after changing the images url’s in an existing website?

    Thank you

    Reply
    • Scott DeLuzio says

      May 17, 2019 at 9:47 am

      It may improve the loading of the site, which Google looks favorably upon. I don’t know how much, if at all, it will help but I don’t see it hurting anything at all. Since the images technically haven’t moved anywhere you won’t have broken links elsewhere on the web (Google image search, or if an image is linked to from another site), so I don’t think you’ll go down at all in search rankings by doing this.

      Reply
  38. Adrian says

    June 14, 2019 at 1:40 am

    I made this changes and works fine, my wp is installed in a subdomain, but now I have a “https:” folder like this:
    public_html/wpsubdomain/https:/static.example.com/uploads
    I uploaded media from admin media library and it was uploaded correctly to /wpsubdomain/wp-content/uploads/ and points to https://static.example.com/media.jpg
    Where that folder and subfolders come from?

    Reply
  39. Arsie Organo Jr says

    September 2, 2019 at 6:02 am

    I have this setup on my site when it was hosted in HostGator. However, when I moved recently to Cloudways, creating a subdomain to host all the images (or even the scripts) isn’t possible. You can set it up but you won’t be able to upload new images. Too bad.

    Reply
  40. BRAIN D Herndon says

    January 11, 2020 at 7:36 am

    Very useful Post

    Reply
  41. Paresh says

    May 7, 2020 at 1:20 pm

    Very helpful and detailed tips, thank you sir, now I am thinking to move images on a subdomain

    Reply
  42. Julien says

    May 8, 2020 at 8:27 am

    Hello, the SQL query does not work:
    # 1146 – The table ‘website_wp419.wp_posts’ does not exist.

    What to do ?

    Reply
  43. Matt Wynne says

    August 2, 2020 at 12:50 am

    Hi there scott,

    Great article thanks for writing.

    I am having the same issue david had way back in 2014, when inputting the mysql command i receive this error

    Table ‘insdebz_db5697.wp_posts’ doesn’t exist

    any ideas?

    Thanks,
    Matt.

    Reply
  44. Miguel says

    October 27, 2020 at 5:18 pm

    This is not working for me. I make the changes to http://mysite.com/wp-admin/options.php but after clicking Save, nothing gets saved. Next time I upload something, the path is the old one and if I go back into http://yoursite.com/wp-admin/options.php I can see the field is blank again.

    I’m using the default wp-content/uploads folder for uploads. I just want to change the path so they are served from a different subdomain, but I don’t want to change the location to which they are actually uploaded.

    Reply
  45. sivilsea says

    February 11, 2021 at 10:14 am

    Hi Scott,
    how can serve css from subdomain in wordpress?

    Reply
  46. Jagdish says

    April 24, 2022 at 7:55 am

    Hi guys,

    I have run this command and want to undo this.

    Enter the following code in the query box:
    UPDATE wp_posts SET post_content = REPLACE (post_content,’http://yoursite.com/wp-content/uploads/’,’http://img.yoursite.com/’);

    Please help, this is urgent.

    Reply
    • Scott DeLuzio says

      April 25, 2022 at 9:27 am

      You should be able to revert back to the backup you made before making any changes. That would probably be the quickest and most effective way of undoing this.

      Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Come say hi!

  • Facebook
  • GitHub
  • Instagram
  • LinkedIn
  • Twitter
  • YouTube

Tweets by scottdeluzio

My Products

Conditional Checkout FieldsFull Screen Background ImagesQuick CheckoutWP1099

I use affiliate links throughout this site and may earn a commission if you purchase through my links. I do not link to products or services that I do not trust, or use myself.
© 2025 · Scott DeLuzio · Built on the Genesis Framework