How to Move WordPress Media Folder to Subdomain?

Each image on a web page sends a separate HTTP request to server thus reducing the speed of response if all images and media files are stored in the same domain. Hence it is a good practice to host images on a separate subdomain so that they will be loaded in parallel requests when the page is loaded. In this article we explain the step by step process on how to host images from subdomain in WordPress.

If you are looking for moving uploads folder to separate folder outside /wp-content/ folder then check out this article.

Move WordPress Media Folder to Subdomain

The entire process consists of the following steps:

  1. Create a subdomain
  2. Setup DNS
  3. Migrate images to subdomain
  4. Change uploads directory
  5. Change live image URLs
  6. Delete or change old uploads folder
  7. Setup redirect for old images

1. Creating a Subdomain

Login to your hosting cPanel or any equivalent account and create a subdomain with the name you want. Below are the steps in Bluehost cPanel and you should have a similar process with other hosting companies as well. Nowadays, SiteGround and other hosting companies use Site Tools which is similar to cPanel.

Navigate to “domains > subdomains” in your cPanel. Enter the subdomain name for the main domain and click on create button. For example, we have created a subdomain img.webnots.com; you can use any names like media.yourdomain.com.

Creating Subdomain in Bluehost
Creating Subdomain in Bluehost

2. Check DNS Settings

Once the subdomain is created ensure the corresponding A records are created in your DNS. In Bluehost you can check this under “domains > zone editor”.

3. Migrating Images to Subdomain

This step is required only for those who already have images on the site stored on different directory like “/wp-content/uploads/”. If you have new site not having any existing images then this step is not required. Launch your FTP client and download the entire folder where the images are stored to your local computer. Then upload all the images into the newly created subdomain folder.

Now that you have duplicated all images to the subdomain and the next step is to configure your WordPress installation on main domain to use the subdomain as an upload folder for media files.

Warning: Uploading images through FTP will not visible in the media folder. Check our article on how to fix FTP uploaded images not appearing in media library.

4. Changing Upload Folder to Subdomain

Login to your hosting account and launch “phpMyAdmin” section under cPanel. Open the database of your main domain. Select “wp-options” table and replace the following two parameters and save your settings.

upload_pathdirectory root for your subdomain
upload_url_pathactual URL path for your subdomain

For example:

upload_path/home/server_root/publich_html/img
upload_url_pathhttp://img.yourdomain.com

Server_root is the root directory of your server, it is the hosting account name in case of Bluehost. If you can’t find the server root then add the below code in any of your WordPress PHP template and copy the output.

<?php echo $_SERVER; ?>

Until this step there will be no impact on your live site since the existing media upload folder is not changed. Any new image you upload henceforth will be stored in a new subdomain path you have entered in the “wp-options” table. You can cross check this by uploading a test image and check the URL.

Once you modified the upload path in “wp-options” table you can see the upload path options are enabled in WordPress admin frontend under “Settings > Media“. Henceforth you can update the uploads file path directly here.

Upload Path is Enabled in WordPress Admin Interface
Upload Path is Enabled in WordPress Admin Interface

Warning: It is recommended to back up your database before doing this step.

Plugins and themes will use this uploads path. Hence, all cached folders, minified files and other files will also be saved in the new subdomain instead of “/wp-content/uploads/” folder.

5. Changing Live Image URLs

This step is not required if you don’t have any existing images on the site. For those have already images on the site you need to change the pointer from current folder to the newly created subdomain path. In order to do this open “phpMyAdmin” and select the database of your main domain. Under SQL tab run the below query:

UPDATE wp_posts SET post_content = REPLACE(post_content, 'URL of current media folder', 'URL of new media folder')
  • URL of current media folder – http://www.domain.com/wp-content/uploads
  • URL of new media folder – http://img.domain.com

This will change all existing image URLs to point to new subdomain. That’s it!!! You are done and check your site is loading with the images from subdomain by checking the URL of an image on live site.

6. Delete or Change Old Media Folder

Once the images are loading from subdomain then you can delete all images in old media folder (/wp-content/uploads/). You can also change the folder name (like /uploads_old/) to retain the images for some more time on the main domain for safer side.

7. Redirecting Already Indexed Images

All existing images might have been indexed by the search engines with old URLs which will now result in 404 error since you have changed/deleted the old folder and moved your images to new subdomain. Here you need to setup a permanent 301 redirect as below in .htaccess file of your main domain.

RewriteCond %{HTTP_HOST} ^yourdomain\.com$ 
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$
RewriteRule ^wp\-content\/uploads\/?(.*)$ "http\:\/\/subdomain\.yourdomain\.com\/$1" 

Replace subdomain and yourdomain with your own values.

This will ensure all image URLs are redirected to the new subdomain and you are not loosing any traffic from search engine’s image search.

8. Creating Home Page for Subdomain

There is one last step we would recommend – adding a home page for your subdomain. At this point of time when you try to reach your subdomain (like img.webnots.com) it will show 403 access forbidden error which may not look professional. Refer our separate article on how to setup a home page for your subdomain to provide some useful details to those viewing your subdomain URL directly on the browser.

Note: You also can use additional folders inside subdomains like “img.webnots.com/files/”.

If you want your images classified under monthly folders then check the box under “Settings > Media > Organize my uploads into month- and year-based folders”. Then the image URL will look something like “img.webnots.com/2015/06/image-name.jpg”.

Editorial Staff at WebNots are team members who love to build websites and share the learning with webmasters community. Follow us on Facebook and Twitter.

48 thoughts on “How to Move WordPress Media Folder to Subdomain?”

Leave your response.

  1. Great tutorial!

    Quick question. If I am using https, wherever I see http in your examples would I switch that with https? Or leave it as http and does the system automatically make this change?

    Thank you for the well written tutorial!

    Reply
    • You can replace HTTPS wherever domain/subdomain URLs are used. Do not replace HTTP_HOST in the redirection setup.

      Reply
  2. Great article, the only issue we have now is when we upload a new image we get a 404. We have also implemented the custom homepage for the subdomain, as this just makes sense as the article suggests.

    Reply
  3. i did’t understood this step?
    2. Check DNS Settings
    Once the subdomain is created ensure the corresponding A records are created in your DNS. In Bluehost you can check this under “domains > zone editor”.

    Reply
  4. Hi, thanks for the great article

    What if domain and subdomain are on different servers?

    Or in my own case, my main domain is on one server but the second domain (which I’m looking forward to only serve uploads from) is on another server.

    Is there any way I can do so without ftp or other problems?

    I’d appreciate any help – thanks

    Reply
    • You need to configure the server to get access from your main domain to subdomain. Otherwise, you will not have permissions to upload content across servers.

      If you are not able to do, probably you need to discuss with your host. This is also easy if both sites are on the same host.

      Reply
  5. Hey there,

    do you mean with replacing upload_path literally by replacing/renaming it with the url path?

    I’m an absolute non phpmyadmin-user so sorry if my question doesn’t sound very intelligent :D

    Thank you in advance :)

    Reply
    • If it shows internal server error, you need to contact the hosting company. Also ensure your domain and sub-domains are on the same server.

      Reply
  6. Hi! I did all the steps written in the article. For some strange reason I didn’t had to go thru step 5 because the images started showing from the subdomain. Did a test like uploading a picture and checking the url source of the already posted images from my articles and still no issue. Any idea why it worked without step 5? Should i still run that command line in my DB from step 5 or should i let things the way they are? Cheers!

    Reply
  7. Hello
    1. Thanks for this really good article
    2. sorry for my bad English Lang
    3. What about xampp:
    I setup a virtual domain (for example https://webnots.com) in xampp and install wp on it
    Then I establish a subdomain named “dl” (dl.webnots.com)
    I check that to making sure it’s working by putting simple index.html to “dl” DIR
    Then I follow this guide step.
    Now the question is:
    1. What is upload_path in xampp
    2. How to config xampp server to allow write access to wp.

    Reply
    • You can’t upload media files from localhost to real server. If you want to have everything locally then enter your local file where you want to store images.

      Reply
  8. Hello, thanks for the tutorial. I actually followed the steps above and everything went smoothly but the only problem I’m facing now is the images are not displaying in the frontend of the site if I set them as featured image. It only affects newly uploaded images, the older ones I migrated are showing fine. Please, what could be the problem?

    Reply
    • You check the file permissions especially when you arranged the media files in monthly folders. Sometimes the new folders may not have sufficient privileges to show the images to public.

      Reply
  9. Hi
    I use the virtual host cpanel according to the article settings, can be used normally and the upload media will be stored in the subdomain folder.
    However, in addition, I used the LINODE host to use the BT.CN panel settings, and found that the media image has been transferred to the subdomain. The foreground page image is normal.
    However, there is a problem that uploading a picture using the wordpress media library does not create a directory: xxxxx. Is the server allowed to write to the upper directory?
    Does this method also apply to VPS niginx.

    Reply
    • At this point, even this site is hosted on VPS nginix and the images are hosted in separate subdomain. So your question is already answered with this.

      For writing permissions – check the subdomain is writable. If the media is arranged in month-year structure, sometimes when the new directory is created for the new month, it will not be in writable mode. You need to change the permission.

      Reply
  10. When you say to check the DNS settings to ensure the corresponding A records are created, what exactly will I be looking for? I have the subdomain created and all of the images are copied into it, just didn’t want to move forward without knowing what the DNS settings need to be. Thanks for this article. All of the others I’ve found online are old and still refer to the media settings which no longer give the option to name a new upload path.

    Reply
    • If primary and subdomains are on the same host then there should be no problem. The DNS records for subdomain will be automatically created when you add a subdomain.

      What you can do is to check that the subdomain is accessible and has correct file permission to write from the primary domain.

      Reply
  11. I have two problem,

    1: Upload path not changed and also path is not showing in media general settings

    2: I got error in SQL step, so tell me is there any other way ?

    SQL query:

    UPDATE wp_posts SET post_content = REPLACE(post_content,’http://www.maghow.com/wp-content/uploads’,’http://img.maghow.com’)
    MySQL said: Documentation

    #1146 – Table ‘exampledb_posts’ doesn’t exist

    Reply
    • 1. If the code is not working, you can use plugin to change the media upload path. There are many plugins out there for doing this.

      2. It seems, you have changed the table prefix from wp_ to something. In that case you should use the correct table prefix what you have changed.

      Reply
    • We did not test on multi-site, ideally it should work if you have configured the media upload path for all the subdomains.

      Reply
    • That’s the same point explained in step 4.
      Once you have decided to host images on subdomain then better do the things directly instead of plugin.

      Reply
    • If it is a cPanel then there will be a default option for creating subdomains. Otherwise you need to ask your hosting company.

      Reply
  12. Apparantly my reply before was not posted because I used the wrong email address.

    Can this be used to share media files between several WordPress websites?
    I have more websites (about 8) that are almost similar except for the languages. They use the same image files.

    And second: some websites are on another server. Could I share files between them or do you have an idea how this can be accomplished?

    Thank you for yor article and looking forward to your reply.

    Reply
    • We did not try this. Ideally this is a subdomain of a one single site – so it may not work across domains and servers.

      Reply
  13. Hi,
    I am not a programmer so I have a question.

    If my images are in a folder in the sub domain how the code in the htaccess should be in the rewriterule part of the code?

    “http\:\/\/subdomain\.yourdomain\.com\/$1”

    Thanks

    Reply
    • The code looks correct with the sub-folder, please check the upload is working under the correct folder after modifying.

      Also you need to provide the correct path in step 4.

      Reply
  14. Hi, thank you for your post!
    The SQL query changing live image URLs did not work for me (WP 4.5.3). This is slightly updated which works:
    UPDATE wp_posts SET post_content = REPLACE(guid,’http://example.com/wp-content/uploads’,’http://subdomain.example.com’) WHERE post_type =’attachment’

    Thanks!

    Reply
  15. Hi Guys

    First of all,thanks for the tutorial …” very clear and simple to follow.”

    my friend said its best to just create a subdomain and use /wp-content/uploads as the document root so you only serve the images from there rather then moving the location

    so can you tell me the benefits over:

    1. actually moving the images completely ( your tutorial)

    Vs.

    2. Just Serve the Images From Sub-Domain & update existing images to point to the sub-domain.

    kind regards

    J

    Reply
    • Before changing the pointer of the images, all existing images should be moved to the sub-domain. So all images (old + new) should be in one place which can be either a sub-domain or folders of a sub-domain.
      Hope this helps.

      Reply
  16. Now
    Point 4. Changing Upload Folder to Subdomain can be easily done by the plugin
    “WP Original Media Path”. Plugin will add path change functionality under “settings>Media” in admin panel. I have applied it on my site and is working fine.

    Reply
  17. Thanks for the article, it works like a charm and surprisingly the page speed in pingdom also increased from 74 to 95.

    Reply

Leave a Comment