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

29 Comments Already

  1. 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.
  2. 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.
  3. 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>