» » » You are reading »

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

51 Comments Already

  1. very complicated tuts for beginners. you have lot of missing steps. i do it everything going good but images are not showing.

    Public CommentUservery complicated tuts for beginners. you have lot of missing steps. i do it everything going good but images are not showing.
  2. Hello, i have an error on uploads files. Could not create the files directory. Make sure the server has permissions to the parent directory. Help Me

    Public CommentUserHello, i have an error on uploads files. Could not create the files directory. Make sure the server has permissions to the parent directory. Help Me
    • did you chmod the upload dir to 755?

      Member of DEZZAIN.COMAdministratordid you chmod the upload dir to 755?
      • yes chmod the upload dir to 755 but same error. Unable to create directory img/2015/04. Is its parent directory writable by the server?

        what should i do?

        Public CommentUseryes chmod the upload dir to 755 but same error. Unable to create directory img/2015/04. Is its parent directory writable by the server? what should i do?
        • did you create the directory in same domain like:
          //www.yourdomain.com
          //img.yourdomain.com

          the upload dir must be in same domain.

          Member of DEZZAIN.COMAdministratordid you create the directory in same domain like: //www.yourdomain.com //img.yourdomain.com the upload dir must be in same domain.
          • i am unable to understand. can you explain it clearly?

            Public CommentUseri am unable to understand. can you explain it clearly?
          • yes i create directory in same domain name.

            //www.mydomain.com
            //img.mydomain.com

            Public CommentUseryes i create directory in same domain name. //www.mydomain.com //img.mydomain.com
          • the directory should include a folder like img/uploads/2015/04 (assuming img is your subdomain directory folder)
            and the /uploads/ folder should be chmod 775
            i don’t think it work with img/2015/04

            Member of DEZZAIN.COMAdministratorthe directory should include a folder like img/uploads/2015/04 (assuming img is your subdomain directory folder) and the /uploads/ folder should be chmod 775 i don't think it work with img/2015/04
          • my directory contain img/uploads
            Upload is the folder which i copies from wp-content to img(durectry/folder/subdomain).

            I already give it a cmd 775 but still same issue.

            Public CommentUsermy directory contain img/uploads Upload is the folder which i copies from wp-content to img(durectry/folder/subdomain). I already give it a cmd 775 but still same issue.
          • try re-track each step. most user use the same method but got it right.
            might be path not correct when config the wp upload path like mention in post.

            Member of DEZZAIN.COMAdministratortry re-track each step. most user use the same method but got it right. might be path not correct when config the wp upload path like mention in post.
          • i have added /home/ftpserver_root/public_html/img and http://img.domain.com in option_value.

            or i have to write it in option_name?

            Public CommentUseri have added /home/ftpserver_root/public_html/img and http://img.domain.com in option_value. or i have to write it in option_name?
  3. Excuse me, im using wp 4.1.1 version, i’ll try to move upload media to other domain ex. my domain is domain.com upload file is file.otherdomain.com/file. Maybe its some case but im using other domain not same domain or sub domain on same domain. somebody can help me?

    what i need to do? FTP or some trick?

    Thanks for help.

    im new in wordpress and php

    Public CommentUserExcuse me, im using wp 4.1.1 version, i'll try to move upload media to other domain ex. my domain is domain.com upload file is file.otherdomain.com/file. Maybe its some case but im using other domain not same domain or sub domain on same domain. somebody can help me? what i need to do? FTP or some trick? Thanks for help. im new in wordpress and php
    • you can’t move upload directory to another domain, must be the same domain but inside sub-domain.

      Member of DEZZAIN.COMAdministratoryou can't move upload directory to another domain, must be the same domain but inside sub-domain.
  4. It is good to move wordpress uploads to sub-domain. But the above process seems to be a bit complex for newbies and non-programmers. Here is the simple approach to achieve this.

    Public CommentUserIt is good to move wordpress uploads to sub-domain. But the above process seems to be a bit complex for newbies and non-programmers. Here is the <a href="http://www.webspeaks.in/2015/01/move-wordpress-content-folder-subdomain.html" rel="nofollow">simple approach</a> to achieve this.
    • @Arvind,

      Could you tell me if it is possible and what the steps would be to set up WordPress (in Windows/IIS) and force/redirect only the wp-content/uploads folder to a completely different Windows file server? The need is to have all the uploaded files on a centralized file server rather than a single web server’s local storage, which is limited.

      What we initially tried was to create a Virtual Folder in IIS for the uploads folder which points to the file server, but WordPress only wants to write to the local file system of the web server it’s installed on, either within wp-content or just above. WordPress doesn’t seem to see the IIS Virtual Folder at all. This is on an internal web site, so I’m not certain that a subdomain would apply to this situation. Please feel free to correct me… :)

      Another unsuccessful method was to create a Symbolic Link on the webserver Windows file system for the uploads folder within the WordPress file structure that points to a \\server\share path. Windows sees this fine, but WordPress cannot write to it. It appears to use the webserver’s local IUSR account which can’t gain access to the Window’s domain resources on the \\server\share of the file server.

      I’ve seen similar questions from others wanting to redirect uploads to a CDN solution, and we’re basically wanting the same without the added layer of a hosted CDN. We would like to keep it within the local network but apart from the single web server. A similar thought might be to create an internal CDN-like web site (or http://FTP…?) that only houses the upload folder by itself, but I’m unsure of how to connect the WordPress site to this, since it only seems to want to house its uploads locally.

      Hopefully I’ve clarified enough of the scenario – would you know a solution?

      Thanks so much.

      Public CommentUser@Arvind, Could you tell me if it is possible and what the steps would be to set up Wordpress (in Windows/IIS) and force/redirect only the wp-content/uploads folder to a completely different Windows file server? The need is to have all the uploaded files on a centralized file server rather than a single web server's local storage, which is limited. What we initially tried was to create a Virtual Folder in IIS for the uploads folder which points to the file server, but Wordpress only wants to write to the local file system of the web server it's installed on, either within wp-content or just above. Wordpress doesn't seem to see the IIS Virtual Folder at all. This is on an internal web site, so I'm not certain that a subdomain would apply to this situation. Please feel free to correct me... :) Another unsuccessful method was to create a Symbolic Link on the webserver Windows file system for the uploads folder within the Wordpress file structure that points to a \\server\share path. Windows sees this fine, but Wordpress cannot write to it. It appears to use the webserver's local IUSR account which can't gain access to the Window's domain resources on the \\server\share of the file server. I've seen similar questions from others wanting to redirect uploads to a CDN solution, and we're basically wanting the same without the added layer of a hosted CDN. We would like to keep it within the local network but apart from the single web server. A similar thought might be to create an internal CDN-like web site (or FTP...?) that only houses the upload folder by itself, but I'm unsure of how to connect the Wordpress site to this, since it only seems to want to house its uploads locally. Hopefully I've clarified enough of the scenario - would you know a solution? Thanks so much.
      • correction:

        “CDN-like web site (or http://FTP…?)” should read:

        CDN-like web site (or FTP site?)

        Public CommentUsercorrection: "CDN-like web site (or http://FTP…?)" should read: CDN-like web site (or FTP site?)
        • Hi,
          If you want to move only the upload folder, you can change it in the wp-config.php as follows:
          define( 'UPLOADS', 'blog/wp-content/uploads' );

          This path can not be absolute. It is always relative to ABSPATH, therefore does not require a leading slash.
          I hope it is helpful.

          Public CommentUserHi, If you want to move only the upload folder, you can change it in the wp-config.php as follows: <code>define( 'UPLOADS', 'blog/wp-content/uploads' );</code> This path can not be absolute. It is always relative to ABSPATH, therefore does not require a leading slash. I hope it is helpful.
          • Thanks – this I already know and have tested successfully. However, this only redirects the folder elsewhere within the local file system of WordPress. I need instead to redirect to a completely different server. I too have also already seen the line from wordpress.org, “This path cannot be absolute.”, but I’m still hoping there is a way to accomplish what I’m attempting.

            Anyone know of a way?

            Public CommentUserThanks - this I already know and have tested successfully. However, this only redirects the folder elsewhere within the local file system of Wordpress. I need instead to redirect to a completely different server. I too have also already seen the line from wordpress.org, "This path cannot be absolute.", but I'm still hoping there is a way to accomplish what I'm attempting. Anyone know of a way?
  5. I did everything as you said. everypost is showing images. but media folder showing broken images.

    By checking url’s i found that
    in posts – http://def.yupmovie.com/uploads/2014/12/404-error.png

    in media – http://def.yupmovie.com/2014/12/404-error.png

    help me out asap

    Public CommentUserI did everything as you said. everypost is showing images. but media folder showing broken images. By checking url's i found that in posts - http://def.yupmovie.com/uploads/2014/12/404-error.png in media - http://def.yupmovie.com/2014/12/404-error.png help me out asap
  6. everything was good.
    I have 2 doubts.
    1)
    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]

    In this code, do i need to replace my domains names? if so, which one main or sub domain?

    2) I don’t want to make my URL for my images as month/date/ title. i just want to use the image name in the image url.

    Public CommentUsereverything was good. I have 2 doubts. 1) 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] In this code, do i need to replace my domains names? if so, which one main or sub domain? 2) I don't want to make my URL for my images as month/date/ title. i just want to use the image name in the image url.
  7. Hi, It works ok but the images in img tag are not showing up it in the page.
    For example not show up the image, but if I type the url in the browser, I am redirected correctly to img.mydomain.com/img.jpg.

    What’s the problem?
    Thanks.

    Public CommentUserHi, It works ok but the images in img tag are not showing up it in the page. For example not show up the image, but if I type the url in the browser, I am redirected correctly to img.mydomain.com/img.jpg. What's the problem? Thanks.
    • check htaccess if you have block image hotlinking..

      Member of DEZZAIN.COMAdministratorcheck htaccess if you have block image hotlinking..
  8. hi richie, does the subdomain need to use a different ip address from the main domain in order for this to work?

    Public CommentUserhi richie, does the subdomain need to use a different ip address from the main domain in order for this to work?
    • also…do i need WP multisite for using a subdomain as a CDN? thanks in advance..

      Public CommentUseralso...do i need WP multisite for using a subdomain as a CDN? thanks in advance..
      • no need, normal installation of wp is sufficient. if use multisite, the upload path tweak would be different.

        Member of DEZZAIN.COMAdministratorno need, normal installation of wp is sufficient. if use multisite, the upload path tweak would be different.
    • no need, as long as it under the same domain. if you have a site domain – mydomain.com, go to cpanel and create a subdomain and name it ‘cdn’ so it would become – cdn.mydomain.com

      Member of DEZZAIN.COMAdministratorno need, as long as it under the same domain. if you have a site domain - mydomain.com, go to cpanel and create a subdomain and name it 'cdn' so it would become - cdn.mydomain.com
      • thanks richie. however, i am still confused. Some tutorials say that the subdomain should be cookieless… so i have 2 questions..

        1. Do you have to make the subdomain cookieless or just leave it after creating?

        2. This article says that if you use Adsense, you can’t use a subdomain because of cookies..you need a brand new domain.

        Public CommentUserthanks richie. however, i am still confused. Some tutorials say that the subdomain should be cookieless... so i have 2 questions.. 1. Do you have to make the subdomain cookieless or just leave it after creating? 2. This <a href="http://www.ravelrumba.com/blog/static-cookieless-domain/" rel="nofollow">article</a> says that if you use Adsense, you can't use a subdomain because of cookies..you need a brand new domain.
        • sorry its this article that says with Adsense, you cannot make a cookieless subdomain… http://www.symkat.com/cookieless-domains

          Public CommentUsersorry its this article that says with Adsense, you cannot make a cookieless subdomain... http://www.symkat.com/cookieless-domains
          • i think you’re confuse with this article. this article teach how to change wp upload path to subdomain and not hosting an website in subdomain with adsense. basically the subdomain is only for image upload, no content whatsoever.

            Member of DEZZAIN.COMAdministratori think you're confuse with this article. this article teach how to change wp upload path to subdomain and not hosting an website in subdomain with adsense. basically the subdomain is only for image upload, no content whatsoever.
  9. 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.
  10. 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. :)
1 2

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>