How to create a subdomain for images of your WordPress blog

Have you ever noticed pagespeed of blog hosting their image files in a subdomain or in a CDN?

Do you want to host your media on a separate sub domain?

Then this post is for you.

Having a separate sub domain for you blog will helps you by:

  1. Boosting Page loading time.
  2. Lowering the bandwidth of the server.
  3. Helps you look more professional.

Requirements:

  • A Self Hosted-WordPress Blog .
  • A Domain.
  • Your domain must support multiple A records.

Ensure that your web host supports you to host subdomains.

Disclaimer: Do the process at your own risk. We are not responsible for any damages.

Creating a sub-domain for your WordPress blog’s images:

What are Subdomains?

Subdomains are the domains which are the child of main domain (Example: www.google.com is the main domain and the images.google.com is the subdomain).

Let’s come to the topic…

Be sure to choose a correct subdomain.You can create a sub-domain on your own by these steps:

  • Go to your domain Cpanel or your domain login.
  • Go to Manage DNS.

Manage DNS

  • Go to A records.

Add A Record

  • Create a new record with the unique IP of your domain. (Your website’s unique IP will found on your Web host cpanel)
  • On the “Host name” field, enter your new subdomain’s address like “img” or “media”.
  • Enter your domain’s IP in the “Destination IPv4 Address”. Finally click on Add Record. (Or follow the image provided here)

Wait for atleast 1-2 hour for your A record to work.

Creating a Directory for your Images Domain:

  • Go to your Host cpanel by entering your URL format like this: “www.yoursite.com/cpanel”
  • Log on to your Cpanel.
  • Click the “File Manager” option under the files section.
File manager
File manager

File manager

  • Go to your WordPress domain root  directory.
  • Create a directory named “Media” or something on the root of your WordPress directory.
  • Click on Save.
You have successfully created a directory for your img domain.
But in case if your previously WordPress uploaded your images on “site.com/wp-content/..” you have to move it. To do this, follow these steps:
  • Go to your “wp-content/uploads/year” directory and move all your content of “wp-content/uploads/year” folder to your newly created “Media” directory.

Creating the Subdomain from your web host:

You have successfully created a subdomain on your domain manager but you have to add this subdomain to your host for this domain to work completely. It’s also a easy process.

  • Go to your Web Host cpanel by entering your URL format like this: “www.yoursite.com/cpanel”
  • Log on to your Cpanel .
  • Go to “Subdomains” option.

Sub Domains

  • On Subdomains, create a subdomain exactly with the name you have previously added in your domain manager. (Ex: if you created a domain “img.site.com” on your first step, then enter the same “img.site.com” on it)
  • On the next field, Enter the address of your previously created “Media” folder.

Now you can successfully created a subdomain on your host.

Creating a main page for your images directory: (Optional)

If you want to create a main page, where users can see when they enter into your images subdomain, follow these steps.

  • Go to “Media” folder on your host.
  • Open a notepad file and enter the below code and save it as “home.html”
  • Insert a simple HTML page on your host like this:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0
Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd”>
<html
xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta
charset=”utf-8″ />
<title>Revealing The Stuffs</title>
<style>body{width:450px;margin:5em
auto;background-color:#fefefe}form{margin:2em 0}
</style>
</head>
<body
onload=”document.search.s.focus()”>
<p
align=”center”><img alt=””
src=”http://lh5.googleusercontent.com/-
SgAdoQCfr30/UBLZuV4dauI/AAAAAAAAAM8/Rukq2
dtDKSA/s586/rts.PNG&gt;&lt;a%20href=” http:=””
revealingthestuffs.com=”” “=””
href=”http://revealingthestuffs.com/” /></p>
<form
action=”http://www.revealingthestuffs.com/” name=”search”
method=”get”>
<div style=”text-align: center”><input
style=”width: 320px; padding: 4px; font-size: 1em;
margin-right: 5px” x-webkit-speech=”” autocomplete=”off”
type=”text” name=”s” /><input type=”submit” value=”Search
Blog” style=”font-size: 1em; padding: 4px 8px”
/>&nbsp;</div></form>
<p><strong><em>You are on
our images directory. You can search our site using the
above search form.</em></strong></p>
<p><strong><em>Our main Blog: <a
href=”www.revealingthestuffs.com”
rel=”nofollow”><strong><em></em></strong></a></em>
</strong></p>
<h2><strong><em><a
href=”http://www.revealingthestuffs.com”
target=”_blank” onclick=”window.open(this.getAttribute
(‘href’),’Revealing The Stuffs’);return false;”
title=””><strong><em>Revealing The
Stuffs</em></strong></a></em></strong></h2><strong
><em><strong><em></em></strong></em></strong>
</
body>
<style type=”text/css”></style>

<p><i><b>Want to create a page like this? Go
to:</i></b> <a
href=”http://www.revealingthestuffs.com”><i><b>Reveal
ing The Stuffs</i></b></a></p> </html>

Note: Edit www.revealingthestuffs.com with your URL.

Upload your “home.html” to your images directory. Now your Main page be available.

Updating WordPress settings:

UPDATE:   WordPress 3.5 didn’t support the Media changing options. So you can’t do the below process. Don’t be panic. You can fix this issue with the use of a plugin. The name of the plugin is “WP Original Media Path“. With the use of this plugin you can add the custom domain like the Media option in previous WordPress versions.

Follow these steps carefully:

  • Go to your WordPress dashboard.
  • Go to Settings > Media.
  • On the “Store uploads in this folder” field enter “Images”
  • And on “Full URL path to files” field enter “img.yoursitesite.com”. Replace “yoursite” with your new images site.
  • Click on save.
Updating Media Options

Final step: (No need for Newly started blogs)

You have completed  a quarter of your work. Now you can complete this process by updating your SQL database.

Updating your old post’s image to the new location:

First you have to update the SQL for updating your Old post’s image to the new location.

  • Go to phpMyAdmin

phpMyAdmin

  • Go to your database and click SQL.Click SQL
  • Enter this code:

UPDATE wp_posts SET post_content = REPLACE(post_content,

'http://www.yourdomain.com/wp-content/uploads','http://img.revealingthestuffs.com/')

Replace the www.yourdoamin.com/wp-content/uploads and img.revealingthestuffs.com with your links. And Click Go. 

SQL Query

Updating your previous posts image links with your new images link:

You have to updated your previous images directory to new. But you have to redirect your previous images link with your new link to work.

  • First go to your File manager.
  • Find “.htaccess” file.
  • Add this following (It’s a good practice to backup your .htaccess file before editing)

RedirectMatch 301 ^/wp-content/uploads/(.*)$ http://img.revealingthestuffs.com/$1

Update my link with yours. Now your images be successfully redirected to your new sub domain.  

That’s it:

Stuck on any step? Tell me your problem by commenting.

34 Replies to “How to create a subdomain for images of your WordPress blog”

  1. First to @Sahil . Bro , obviously we can create sub-domains from the cPanel itself but how will you link the sub-domain to your hosting , which means that adding a “A” record is compulsory . You can’t store files in a sub-domain without having an “A” record for it . Even though without adding “A” record we can get the url (Example:- subdomain.techravers.com/image.jpg) , we can’t upload a html file to it until a “A”record is added .

    Coming to you @Sriram . Man , this post is really awesome . In the beginning I thought I could not achieve what I wanted but I really did achieve it but I had to give it up due to some technical issues . Loved the post by the way , who gave you the reference to this post . Did you figure it out on your own .

  2. Just to keep you informed Sri Ram , this method cannot be used in WordPress 3.5 . Because in the Media Tab there is no option for updating default upload location .

    You have to insert some PHP coding to change the place where it must be uploaded .

  3. Everything seems to work except the fact that primary domain could not create sub folders for uploaded media : (
    How to fix avoiding the manual creation ?

      1. Hi, thanks for your fast respond..

        Just finished all steps explained above. But all images isn’t showing on my site now.
        Maybe I should wait the propagation time for my new subdomain.

        After copying all images, is it safe to delete years-folders on my old upload folder?

        regards.

  4. @Sriram

    I have my domains registered with 1and1. 1and1 doesn’t have cPanel, so I host my domains with a hosting company that uses cPanel. When I create a subdomain on my cPanel and go to my Advanced DNS Zone Editor, there is already an A record there… without me creating a subdomain at 1and1 (my domain registrar). Did I miss a step?

    1. Hi Bill,
      I need clarification. Did you transfer your domain from 1and1 to another domain provider? Or you are just pointing your domain name to 1and1 server?

        1. Hi Bill,
          Your domain name provider need not have a cPanel. Even my domain name provider, Big Rock doesn’t have one. You just have to edit the A records in DNS management of 1and1. Reply if I’ve solved your problem.

          1. Hi Sriram,

            When I set my domains up with my domain name provider 1and1, I set them up with Nameserver 1 and Nameserver 2 addresses, not with an A Record. When I add subdomains to my main domains in my domain name provider 1and1, the subdomains use the same DNS info as the main domain. The A Records for my subdomains in the 1and1 Control Panel DNS Management aren’t editable fields. I added subdomains in my hosting providers cPanel and the A Record fields were automatically populated in the Advanced DNS Zone Editor. My domains and subdomains all have the the same A Record with both my domain name provider 1and1 and my hosting provider.

          2. I hope you can create A records in your domain name provider 1and1. So, go to ‘A records’ and create a new one with your desired sub domain name. And go to your web host’s cPanel and find the IP address of your web server. Copy that address and return to 1and1 ‘A records’. Paste the IP address that you’ve copied from cPanel in ‘Destination IPv4 Address’. Now you’ve pointed your new sub domain to your web host. Refer this image http://img.revealingthestuffs.com/2015/a-record.PNG

            Now follow the post from ‘Creating a Directory for your Images Domain’ section.

    1. Thanks for the screenshots. I think you’ve done everything correctly. As your main domain name is linked to your web host, your subdomains too will point to them. So now you’ve linked subdomain to your web host, try accessing your subdomain using your web browser.
      You’ve to create subdomain in 1and1 as well as in web host with the same name.

      1. Sriram,

        Thanks for your help! I’ve created identical subdomains at both my domain provider (1and1) and my webhost, plus I can access my subdomains in my web browsers. So I guess my subdomains are being seen as different websites from my main domains and my pages are loading faster as a result?

        Are you implying that a main domain can be hosted on one server and it’s subdomains hosted on another by using A Records, when you state “As your main domain name is linked to your web host” and “Your domain must support multiple A records.”? I’m guessing an alternate way for me to have set my domains up, would have been to use 1and1’s name servers and used the webhost’s IP Address as an A Record?

        1. Let me tell you the process for your understanding. You’ve your domain name in 1and1 and you want to use a separate web host to host your files. And now there is no link between 1and1 and your web host. But you know the IP address of your web host. So you go to 1and1 and edit your domain’s A record to link your domain at 1and1 to your web host. Now your domain name is linked to your web host and you can view your files uploaded to your web host in your domain name.

          Now you create a sub domain. In my case, I created a sub domain by creating a A record. And I need my sub domain too to link to my web host. That’s why I asked you to enter the IP address of your web host for your sub domain. Now your sub domain links to your web host.

          But your web host don’t know what to do with your newly created sub domain at 1and1 as your web host don’t know anything about your sub domain. So now you need to create a sub domain in your web host (cPanel) and make it point to some directory inside your web host. The reason why I asked you to enter exactly the same name in 1and1 and web host is because when both of them are identical your domain name at 1and1 now directly link to the directory you’ve pointed your sub domain to in your web host.

          Hope this helps.

  5. hi sriram…

    post was excellent 🙂

    please post a tutorial about mapping(park) subdomain in 000webhost.com

    i have a blogspot blog (tutorials.blogspot.com) and i have setup this blog with bigrock as “www.tutorials.com” but for demo tutorials i want to setup sub domain, i have created a subdomain in bigrock as “demo.tutorials.com” , and i want to host my demo tutorials in 000webhost(free webhosting service) as it is a free webhosting service, i have created here “demo.tutorials.tk” freely its working,

    but how can i park bigrock’s subdomain into 000webhost, hosting in 000webhost and sub domain in bigrock..

    i want to show content tutorials of 000webhost to “demo.tutorials.com”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.