How to Move WordPress Uploads Path to Subdomain

I’m a big fan of site loading fast, for me when i visited a site, it took only 2-4 second only to stay on before leaving if the site loading were slow. Doing some testing on Dezzain.com for speed and load time and found out the site speed and load time are at average. so i begin to research and read articles about ‘how to make you site load faster’, ‘using CDN as content storage’ and ‘how to reduce your bandwidth usage’
As tempting as a Free CDN Service to me, i would like to try on hosting my WordPress uploads folder in my own sub-domain. I want to know how much bandwidth and server load can be reduce in comparison before moving on to Paid or Premium CDN Services.

You might found that there’s already a tons of tutorial on how to host your images in sub-domain in Google search so here’s another step by step tutorial that teach you how to properly move WordPress Uploads Folder and Path to your sub-domain.

1. Backup First is the Safest Way

Don’t be a hero, you need to backup all necessary file first. If anything goes wrong, you’ll definitely wish you have a time machine to go back in time :) First, go to your web-hosting control panel, under database tab, there’s should be a phpMyAdmin button. Click it and this will take you to phpMyAdmin control panel. Click the export tab and this will appeared on screen. Now export the database using a SQL Format -> Select All and only edit/checked Add DROP TABLE / VIEW / PROCEDURE / FUNCTION / EVENT statement and leave other options as default.

phpMyAdmin

Save the SQL file to your desktop. For a larger database, it may take longer time to download so be patient. While you at it, after saving the SQL file, copy another one to other location just for safe keeping. you’ll never know!

2. Creating a Sub-domain

If you do not know what sub-domain is? Sub-domain is a domain within a domain. Unlike sub-directory, sub-domain had its own domain for socket HTTP/HTTPS requests. That’s why using sub-domain to host content and images will reduce your bandwidth and HTTP/HTTPS request time.

Example
http://www.domain.com - this is a main domain http://subdomain.domain.com - this is a sub-domain

as for sub-directory
http://www.domain.com/sub-diretory/ - this is a sub-directory

Log into cPanel and click on the “Subdomains” icon under the “Domains” section. Next, fill in the subdomain name you want to create and click “Create”. Assign user access privilege if necessary.

Here we will be using ‘img1′ as our subdomain so the later sub-domain will be like
http://img1.domain.com

Login to your FTP and you’ll see a newly created ‘img1′ folder. Now copy and paste your WordPress uploads folder into the ‘img1′ folder. The new uploads path would look like this
http://img1.domain.com/uploads/

3. Editing the MySQL for the new uploads path

Now there is 2 way to edit the saved MySQL database file in step one. one is using the phpMyAdmin SQL panel and second using any text editor to edit and replace. I find that the second is better. Anyhow i will show you both method.

Method 1 – via phpMyAdmin SQL
Login into your phpMyAdmin go to tab SQL and copy paste this into the textarea.
UPDATE wp_posts SET post_content = REPLACE(post_content,'http://www.domain.com/wp-content/uploads','http://img1.domain.com/uploads')
and after than another mysql to update the attachment path
UPDATE wp_posts SET guid = REPLACE(guid,'http://www.domain.com/wp-content/uploads','http://img1.domain.com/uploads')

Method 1 – via text editor
in this case, i use notepad++. Open the SQL file with notepad++ and click on Search->Replace All on top and enter setup as follow.

edit-via-notepad

sometime you have a url path without www, try search domain.com/wp-content/uploads and if found one, repeat the step and replace with img1.domain.com/uploads

Method 2 – you will need to drop you current wp mysql table and import the newly edit SQL file through phpMyAdmin -> import

Do not worry, if anything goes wrong, we still have the second backups of the SQL file so you can re-import the previous database table.

4. Change the WordPress Admin Options Upload Path

Again go into your phpMyAdmin and click on table wp_options and browse to Search tab. In the search tab enter a search query like below and click go.

search-uploads-phpmysql

Now edit upload_path and upload_url_path to

change-upload-path

upload path to /home/ftpserver_root/public_html/img1
upload _url_path to http://img1.domain.com

you can find out what’s your ftpserver_root by adding this to any WordPress template.

<?php echo $_SERVER["DOCUMENT_ROOT"]; ?>

please note: the upload_path might be varies from other hosting.

5. Using .HTACCESS to redirect old path to new path

To avoid crawling error and bad request 404 for the images and attachments, add this to your WordPress main root .htaccess file.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^wp\-content\/uploads\/?(.*)$ "http\:\/\/img1\.domain\.com\/uploads\/$1" [R=301,L]

credit to chanmyayaung for the root domain htaccess only

Final Note

If everything went to according to plan, you should be able to see all your images and attachment properly link to your new upload path. New upload file in media or posts will automatically upload into img1.domain.com/uploads/

As you can see the current site also using subdomain – cdn.dezzain.com/1/ for all images and attachments uploads. Hope you find the tutorial benefit to your WordPress development. If you found error and bug in the step, feel free to let me know here.

How to Move WordPress Uploads Path to Subdomainhttp://cdn.dezzain.com/1/2013/05/speed-test-dezzain.pnghttp://cdn.dezzain.com/1/2013/05/speed-test-dezzain-150x150.png Richie KS WordPress TutorialsEditor Picks,uploads,uploads path
Step by step on how to move WordPress upload path to sub-domain to increase website loading speed and reduce bandwidth usage, load time and HTTP/HTTPS request

22 Comments Already

  1. Hi!

    Thank you very much! I was looking for this clean solution over 2 years!
    It works like a charm.

    Public CommentUserHi! Thank you very much! I was looking for this clean solution over 2 years! It works like a charm.
  2. OK I used http://img.mydomain.com for my subdomain, and it works. :)

    Public CommentUserOK I used http://img.mydomain.com for my subdomain, and it works. :)
  3. Can this be implemented on a sub-domain? Which means the image is a sub sub domain, like this;

    http://img.subdomain.mydomain.com

    Public CommentUserCan this be implemented on a sub-domain? Which means the image is a sub sub domain, like this; http://img.subdomain.mydomain.com
    • not sure, haven’t tried them under sub sub domain.

      Member of Dezzain.comAdministratornot sure, haven't tried them under sub sub domain.
  4. I tried these steps and made a self CDN on sub domain. It really improves the image loading. I am thinking to put my Static files (CSS and JS) to another sub domain too ? Will it be good idea?

    Public CommentUserI tried these steps and made a self CDN on sub domain. It really improves the image loading. I am thinking to put my Static files (CSS and JS) to another sub domain too ? Will it be good idea?
  5. Hello,

    Nice tutorial, how can i use CDN for it after i moved it to a different folder??

    Public CommentUserHello, Nice tutorial, how can i use CDN for it after i moved it to a different folder??
  6. I’m affraid this way makes google angry. Because in my opinion, redirect sub domain to another hosting means just like guest post. You have one site but with a lot location.

    Public CommentUserI'm affraid this way makes google angry. Because in my opinion, redirect sub domain to another hosting means just like guest post. You have one site but with a lot location.
  7. Hello Richie,

    Thank you for the nice tutorial, I have moved my Upload folder to sub-domain.

    Public CommentUserHello Richie, Thank you for the nice tutorial, I have moved my Upload folder to sub-domain.
  8. How to Move WordPress Uploads Path to other domain or cloud storage?

    Public CommentUserHow to Move WordPress Uploads Path to other domain or cloud storage?
  9. when i am trying to upload new file it show as Not Found in browser view

    Public CommentUserwhen i am trying to upload new file it show as Not Found in browser view
  10. Thanks a ton !

    Public CommentUserThanks a ton !
  11. Very cool tutorial, thanks a lot!

    I encountered an issue however: after I updated the SQL db using “Method 1″, I see that the images within my posts have a double forward slashes in their URLs, e.g. “http:static.finanztreff.org/uploads//image.jpg”

    I double checked the uploads paths in SQL again, but they all look ok (“http:static.finanztreff.org/uploads” – without trailing slash at the end).

    Do you have any idea what might have caused that?

    Public CommentUserVery cool tutorial, thanks a lot! I encountered an issue however: after I updated the SQL db using "Method 1", I see that the images within my posts have a double forward slashes in their URLs, e.g. "http:static.finanztreff.org/uploads<b>//</b>image.jpg" I double checked the uploads paths in SQL again, but they all look ok ("http:static.finanztreff.org/uploads" - without trailing slash at the end). Do you have any idea what might have caused that?
    • Note: I meant of course “http://static.finanztreff.org/uploads//image.jpg” and so for the upload path…

      Public CommentUserNote: I meant of course “http://static.finanztreff.org/uploads<b>//</b>image.jpg” and so for the upload path...
      • check wp-admin/options.php for
        upload_path ->
        upload_url_path ->

        maybe one of this have trailing on end.

        Member of Dezzain.comAdministratorcheck wp-admin/options.php for upload_path -> upload_url_path -> maybe one of this have trailing on end.
  12. Doesn’t seem to work for me. I’ve been trying lot of methods including this one but no luck. Perhaps it doesn’t work in shared hosting.

    Public CommentUserDoesn't seem to work for me. I've been trying lot of methods including this one but no luck. Perhaps it doesn't work in shared hosting.
  13. Thanks! This did the trick.

    So I needed the server root all along!

    Public CommentUserThanks! This did the trick. So I needed the server root all along!
  14. Using url path uploader plugin it did the trick

    Public CommentUserUsing url path uploader plugin it did the trick
  15. Hey! I got the solution

    Public CommentUserHey! I got the solution
  16. Hi thanks for this amazing article.I had created an subdomain but when i want to open it on browser an authentication dialog box appears with username and password fields.It states the site says “DSL Router”.How to overcome this problem please let me know,thnks in advance :)

    Public CommentUserHi thanks for this amazing article.I had created an subdomain but when i want to open it on browser an authentication dialog box appears with username and password fields.It states the site says "DSL Router".How to overcome this problem please let me know,thnks in advance :)
  17. Hi,

    Thanks for the tutorial.
    I want to move whole wp-content folder to my another sub-domain. So, is it possible? If it is, then please guide me.
    Thanks again!


    Akash

    Public CommentUserHi, Thanks for the tutorial. I want to move whole wp-content folder to my another sub-domain. So, is it possible? If it is, then please guide me. Thanks again! -- Akash
    • the whole wp-content folder could not be moved, since it include themes, plugins etc
      only wp-content/uploads are moveable with wp_upload_dir constant functions change

      Member of Dezzain.comAdministratorthe whole <strong>wp-content</strong> folder could not be moved, since it include themes, plugins etc only wp-content/uploads are moveable with <strong>wp_upload_dir</strong> constant functions change
      • Hi Richie,

        Thanks a lot for the tutorial.It worked fine for me.

        Can we also move other static contents to different subdomain for more optimisation ?

        Public CommentUserHi Richie, Thanks a lot for the tutorial.It worked fine for me. Can we also move other static contents to different subdomain for more optimisation ?

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>