Search engines like Google use your sites loading speed as a parameter to ranking your site. The faster your site loads, the better you score and the more chance you have to get a good rank in the search engine results (if your content is good, of course). I’m working on a new product that teaches you how to speed up your WordPress site, but one tip I wanted to share with you today: using a subdomain for images.
Before I walk you through the steps you need to take, let me explain why you should do this for your own WordPress sites. Web browsers will only download 4 files simultaneously from one domain when loading a site. If you use a lot of images in your blog posts this means that it can take some time before all of them are loaded and appear on your site. If your images are on a subdomain, it will decrease the loading time as the browser will load 4 files from the main domain while simulaneously loading 4 files from the subdomain.
Time for the tutorial. Before we start though, make sure that you have a backup ready, at least one from the database, as we will be making changes there and you wouldn’t want to risk making a mistake and losing all your work, right?
Step By Step Approach To Setting Up A Subdomain For Images in WordPress
1. Create a subdomain
First thing you have to do is create the subdomain that will be used for the images. The hosting I’m working with is Hostgator, but if you are using a hosting company that uses cPanel, the steps to create a subdomain shouldn’t be different. If you are not using cPanel and don’t know how to create a subdomain, you should check the knowledgebase of your hosting or contact them for help.
Make sure that you’re logged into your cpanel and look for the icon that is called “Subdomains” in the “Domains” section.
When you’re there you can create a subdomain for any domain that you added to your hosting company, whether it’s your main domain name or an addon domain. In this example I’m creating a subdomain for an addon domain I have on this cPanel. I’m naming the subdomain img, as in http://img.domainname.com.
Instead of creating a new document root for the domain name, I’m pointing it towards the already existing uploads folder of the site I’m creating it for. The root should look like this (if you’re working with a blog on an addon domain: public_html/domainname.com/wp-content/uploads. If you’re working on the main domain the root would look like this: public_html/wp-content/uploads. You can always check in your File Manager to see what you should use for your own site. Make sure that you copy it without mistakes.
2. Change the default image upload location in WordPress
Now that we have our subdomain, we need to tell WordPress that we want to use that to store our images when we’re uploading them through the Media Uploader. Before WordPress 3.5 we could make that change in the Settings >> Media options, but in WordPress 3.5 that option was removed. To make the change now we have to go to http://domainname.com/wp-admin/options.php where you look for a line called upload_url_path. There you can put in the link to your new subdomain.
From now on, new images will basically still be uploaded in the same place as before (important to know for backups, you don’t need to create an extra backup since they’re still part of the files of your main WordPress site), but the link for your images will use the subdomain. On a new site you’re finished with the set up. If you have an older site where you are implementing this, you have to add an extra step. Changing the links for the already uploaded images to the new subdomain. Not that the old image links won’t work anymore, but if you really want to speed up your site, you better make the change for all of them, right?
3. Updating the old image links
This step is a bit more technical and will require you to make changes in your database. If you aren’t too sure about how to handle this, I would recommend outsourcing it to someone who knows what they are doing. In your cPanel, go to “PHPMyAdmin” in the “Databases” section.
When you’re in there, select the database for your blog (if you’re not sure which one is connected to the blog you’re working you can check your wp-config.php file). Once you have the correct database, select the SQL tab:
UPDATE wp_posts SET post_content = REPLACE(post_content,'http://www.yourdomain.com/wp-content/uploads/', 'http://img.yourdomain.com/')
Don’t forget to change yourdomain.com to your own domain name AND if you changed the wp_ prefix for your database, so also make that change in the wp_posts part.
Edited: The complete report with a step-by-step approach to speed up your sites loading speed is done. You can find it here at Easy WP Speed.