how to serve wordpress images from subdomain

creating sub domain for wordpress images is best way to increase your wordpress site speed. We given steps to, how to serve wordpress images from sub domain. Many shared hosting and wordpress bloggers and  websites are using the apache web server for hosting the wordpress sites. Apache Web server is having his own rules and behavior. By default when you open webpage of your website. That time you are making so many requests to apache web server with same domain name. That will create queue to apache web server with same domain name.

how to serve wordpress images from subdomain

For reducing the load time you should keep image file on different sub domain. Means though image subdomain you can send a parallel request to Apache web server. It will reduce the concurrent connections to web server. WordPress provides the functionality to set different domain or sub domain for images.

In this article I will show you how easily you can set the wordpress images from sub domain. First create subdomain through your control panel. Use following step for creating subdomain.

how to serve wordpress images from subdomain
how to serve wordpress images from subdomain

I recommend to create the images subdomain because images subdomain is good for SEO. Here I created the purabtech.in/files/ subdomain for my images. Then set your wordpress images path in document root setting. Normally your wordpress images are stored in wp-content/uploads directory. You can use following setting for setting the document root.

how to serve wordpress images from subdomain
how to serve wordpress images from subdomain

After creating subdomain for images and setting up the correct document root for images subdomain. Go to your wordpress admin panel. From right side panel you should go to settings -> Media panel. This page will look like as follows:

how to serve wordpress images from subdomain
how to serve wordpress images from subdomain

In “Full URL path to files” textbox put your image subdomain name and click on save changes button. Uncheck the organize my uploads into month- and year-based folder option because which is not good for SEO purpose.

After this your images will come from image subdomain but old uploaded image files will still come from older location and same domain. For changing the older images file location and domain information you should use the following steps.

Go to your Mysql databases and select the your wordpress database. You can use the phpmyadmin sql manager for this or you can use command prompt for this if you are having dedicated hosting server. If you taken shared hosting then I recommend to use PhpMyAdmin.

Use following two SQL query: (Note: You should use your images subdomain and your domain name in SQL query)


UPDATE wp_posts SET post_content = REPLACE(post_content,'http://images.purabtech.in/','http://images.purabtech.in/');

UPDATE wp_posts SET guid = REPLACE(guid,'http://images.purabtech.in/','http://images.purabtech.in/');

After this your old images files will point to images subdomain. If your images and wordpress website is old then google and other search engines already indexed your images. So We need to write the redirection rewrite rule for images in your .htaccess file. This file you will find in your Root directory of wordpress installation.

Use following line of code in your .htaccess file.


RewriteEngine On
RewriteBase /

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

Above all steps are important to change the wordpress images into images subdoamin. If you are having any issues or questions about this article. Then please write to me.

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

4 thoughts on “how to serve wordpress images from subdomain”

  1. This is more update from my side for wordpress mu users. use the following mysql query.
    UPDATE wp_posts SET post_content = REPLACE(post_content,’http://wordpressapi.com/files/’,’http://images.wordpressapi.com/’);

    UPDATE wp_posts SET guid = REPLACE(guid,’http://wordpressapi.com/files/’,’http://images.wordpressapi.com/’);

    and use the document root as follows
    domainname.com/wp-content/blogs.dir\1\files

    For wordpress mu users, if you are having any issues then get back to me.

  2. Thanks for this useful article. I just want to ask if you want to load theme images from subdomain, what SQL query should i place?

    Best Regards

  3. This is very nice tutorial for images subdomain in wordpress.The future is very help for page speed(Parallelize downloads across hostnames).

    Thank you very much.

    Regards,

    Kannan.

Leave a Reply to kannan Cancel reply

Your email address will not be published.