How to host more than one website using one domain ?

Power of CNAME records

Jatin Kumar
4 min readMay 7, 2019

Yes It is possible to host multiple websites with different sub domains using one root domain. So first of all what is a root domain and sub domain?
Let’s consider a web address http://www.example.com, so, here example.com is referred to as root domain and www is referred to as sub domain. Figure shown below diagrammatically shows different parts of a web address

Sub parts of a web address

Sub domain can be of many other types also for eg. imap, pop, webmail, etc. (If curious, you can see more of them in CNAME records of your domain) Actually we can define our own custom sub domain for special reasons and we’re gonna do this later in this tutorial indeed.

What you may want to achieve ?

Image shown below, portrays exactly what we’ll be trying to achieve during rest of the tutorial. Feel free to discontinue reading if it’s not the result that you desired. But I would highly recommend you to read it till end, because information discussed below is good to be aware of, at least from the web developers point of view. And most importantly …

Knowledge never goes wasted.

Why people may do this ?

People mostly tend to host multiple websites using one root domain. When they have set of websites which has content related to each other. Let’s say a person has a Portfolio website and Blog. Now suppose domain name is person.com. As both the web pages are related to same person. It would not make much difference if they share root domain and Also it would be even better for the person to have both things located under one domain in an organised manner rather than having separate ones for both. As a result now possible web address for Portfolio and Blog could be http://www.person.com and http://blog.person.com respectively. In this scenario people save a significant amount of money too. So It’s a win win situation.
In coming section we would be getting started with process of hosting multiple Websites under single root domain.

Let’s get started

Before getting started I assume that you have two Dynamic websites, which have been already deployed using Heroku, or any any other free hosting service of your choice. Basically We only need the DNS that they provide.

Generate DNS : In Heroku in order to generate DNS, you need to add your registered domain along with a sub domain (www, blog, me, etc) to your project by going into settings, After entering Domain in the Domain Name Field and press Save Changes, Now heroku would automatically generate DNS related to that domain just entered.

Create a CNAME record : Now go to you control panel of domain and open the DNS Manager. Now click on CNAME records. Here you might get overwhelmed by seeing at amount of different sub domains. Don’t worry we would not be staying here for too long. Now we simple need to Add a CNAME record. For that click on Add CNAME Record. Now a Add CNAME section should open and look similar to figure below. Now fill the sub domain for which you generated DNS in Host Name Field and paste full DNS associated with the mentioned sub domain in Value Field. In above example we used blog as a sub domain. FYI you just created your first sub domain.

Repeat the above two steps for other website also, And keep in mind to have a different sub domain than before. After you are done with both, You need to wait until number of seconds given in TTL expires (You cannot witness live changes until Time given in TTL expires, if possible assign 0 to TTL, so that you can witness live DNS record changes ) . After TTL is expired you can now be able to access two different websites (differing in sub domains) using one root domain only.

I Hope that you liked the Tutorial, I would like to know in comments How you found it. If you wish, you can follow me on facebook and twitter

--

--