How To Show Images from Sub Domain in WordPress

Last Updated On : Sunday, September 6, 2015
0

To get better ranking in Google, one of the major factor is website page speed. Page Speed is one of the part of Google SEO suggestion. So in a wordpress blog we upload a lot of images to make the content more interactive. By default wordpress stores all your uploaded images, documents and other uploads to the wp-content/uploads folder which is part of the same home domain. So while loading all the images and contents from the same domain, it takes much time and page speed is slow. So to avoid this we can deliver the images from a sub domain to increase the page speed as well as SEO ranking. In this post i’m going to share How you can Deliver Images from Sub Domain and how to redirect the older images to the new sub domain URL in WordPress.

Before you start want to see the live example, then right-click on any images in this post, then click copy image url and paste it on the address bar. I have configured my images on my sub domain http://img.techtolead.in.

How To Steps:

Creation of Sub Domain:

First thing you need to do is creating a sub domain under your main domain.

For example the sub domain can be like
http://img.techtolead.in

Name it according to your wish.

How To Create Sub Domain:

Login to your web-host cpanel and navigate to “Sub Domains”.

How To Deliver Images from Sub Domain in WordPress (1)

Enter a name for the sub-domain i.e images.techtolead.in, give the path of the uploads folder which is usually

public_html/wp-content/uploads

and click on “Create”.

How To Deliver Images from Sub Domain in WordPress (2)

Adding a Sub Domain in WordPress:

Once after creating the sub domain, the next step is to add and configure in wordpress. Hence the future uploads will be pointed to the sub domain. One of the sad thing is WordPress 3.5 [or later] versions does not allow to add a domain for the uploads folder. But still you can add a custom domain for the uploads folder using this plugin.

Once after installing the plugin, navigate to “Settings >> Media” in WordPress cpanel.

How To Deliver Images from Sub Domain in WordPress (1)

 

Now under “Uploading files” you can see “Full URL path to files” , Enter your sub domain url without / at the end.

Also if you want to make your images with short url then un-check the “Organize my uploads into month- and year-based folders” check box.

How To Deliver Images from Sub Domain in WordPress (3)

Click “Save Changes”

From now the uploaded files and images will be load from your sub domain.

How To Change the URL of the Older Posts :

Many of the wordpress users including me, has come to know about the sub domain future after starting and posting many contents. So how can we move the older images to load more th new sub domain without losing or re uploading the images. No worries, we can make it using a single line DB update script.

Navigate to the “phpMyAdmin” from your web host control panel.

How To Deliver Images from Sub Domain in WordPress (4)

Now select your wordpress Database and click on “SQL” tab from the nav-bar

Enter the query given below, then click on “Go”. Make sure to replace techtolead.in with your real domain.

UPDATE wp_posts SET post_content = REPLACE(post_content,’http://www.techtolead.com/wp-content/uploads//’,’http://images.techtolead.in/’)

 

Where

  • wp_posts in the table name by default, if you had custom prefix then change it accordingly.
  • http://www.techtolead.com/wp-content/uploads// – is the URL of your current uploads folder. Make sure to replace techtolead.in with your real domain.
  • – is the new URL [ Sub Domain ] of uploads folder

How To Deliver Images from Sub Domain in WordPress (5)

Now the table was updated and changes was made to the older posts.

Redirecting Old Images to the new Sub Domain:

We have already updated the older posts and now we are in last step to redirect the older image URL’s in the older posts to the new one. All you have to do is enter the following code in the .htaccess file and save it. If .htaccess file isn’t available then create one. By default the .htaccess file will be available in your root directory.

RedirectMatch 301 ^/wp-content/uploads/(.*)$ http://images.techtolead.in/$1

Make sure to replace techtolead.in with your real domain.

All the steps complete. Please follow the steps carefully and once done the images in the older posts will open/load with the new sub domain URL without any loss. Also the future posts will have the new URL automatically on images.

I hope this tutorial helped you in improving your WordPress site page speed.

Please share your suggestions and share if any other ways to do the same via comments. Follow Us on Twitter, Facebook, Google+.