article

Dynamic Domain Hosting

Email
Submitted on: 1/1/2015 6:09:00 PM
By: Lewis E. Moten III (from psc cd)  
Level: Beginner
User Rating: By 5 Users
Compatibility: ASP.NET
Views: 1520
 
     Shows how you can host multiple domains under one IP address by mapping domain names to sites within a database.


 
				

Dynamic Domain Hosting

 

I have a fairly large project that I’m working on within my spare time to host my own site, and sites for friends.  I have decided to update to .Net and look at the technology that I can take advantage of.

 

My biggest beef with my current system is that I spend a lot of time setting up new websites.  I would need to add a new website and modify its host header, paste new code in a separate folder, and change settings in the files, etc.  My goal is to cut out the middle man … and that would be me.

 

The idea would be to update my database so that all sites would access the same database catalog.  When a user accessed any of the domains, it would hit the same code, but look for the data associated with the host provided in the URL.

 

Database

 

For the task at hand, I created two tables in my database.

 

Sites

SiteID

Guid

Not null, PKey

Description

Varchar(255)

Null

 

Domains

DomainID

Guid

Not null, PKey

DomainName

Varchar(50)

Not null, indexed

SiteID

Guid

Not null, FKey Sites.SiteID

 

This setup allows me to assign multiple domains to the same site.  For example, my domains may consist of http://www.lewismoten.com and http://lewismoten.com .

 

ASP.Net

 

My next goal was to lookup the SiteID of the domain on a page request.  Rather then hit the database each time, I save the SiteID in a cookie on the first request.

 

Public SiteID As System.Guid

 

Private Sub Page_Load …

If Request.Cookies("SiteID") Is Nothing Then

      LoadSiteID()

Else

      Try

            SiteID = New System.Guid(Request.Cookies("SiteID").Value)

      Catch ex As Exception

            ' problems loading cookie ... bad format?

            LoadSiteID()

      End Try

End If

Response.Write SiteID.toString()

End Sub

 

The code to load the sites identification appears like so …

Private Sub LoadSiteID()

 

      Dim Domain As String = Request.Url.Host

      Dim oConnection As New SqlClient.SqlConnection()

      Dim oCommand As New SqlClient.SqlCommand()

      Dim oDataTable As New DataTable("Sites")

      Dim oDataAdapter As New SqlClient.SqlDataAdapter()

      Dim SQL As String = ""

 

      SQL &= "SELECT SiteID FROM Domains WHERE "

      SQL &= "Domains.DomainName = '" & Domain & "'"

 

      oConnection.ConnectionString = "data source=MyServer;initial catalog=MyDatabase;UID=MyUsername;PWD=MyPassword"

 

      oCommand = New SqlClient.SqlCommand(SQL, oConnection)

 

      oDataAdapter = New SqlClient.SqlDataAdapter(oCommand)

      oDataAdapter.Fill(oDataTable)

      If oDataTable.Rows.Count() = 0 Then

            SiteID = System.Guid.Empty

      Else

            SiteID = CType(oDataTable.Rows(0).Item(0), System.Guid)

      End If

 

      ' If site could not be found within database ...

      If SiteID.CompareTo(System.Guid.Empty) = 0 Then

 

            ' Perhaps goto page for setting up new domains

            Response.Write("Domain has not been mapped.”)

            Response.End

 

      Else

 

            ' Save SiteID in cookie

            Response.Cookies("SiteID").Value = SiteID.ToString

            Response.Cookies("SiteID").Expires = DateAdd(DateInterval.Year, 5, Now())

            Response.Cookies("SiteID").Path = "/"

 

      End If

 

End Sub

 

From here you can see how I tell the user if the domain doesn’t exist within the database.  It is possible to expand this code so that the user is sent to a setup page so that they can add there domain and/or site to the database.

 

Testing

 

To test the code on your local computer, you may need to modify your hosts file.  Under Windows NT/2000/XP, you may find this file under your Windows folder by following the path to “System32\Drivers\etc\hosts”.  The Hosts file does not have a file extension.  If you click on it, you will be prompted to select a program to open the file.  Scroll down the list until you locate a text editor such as notepad.

 

You will see comments within the file instructing you how to setup mappings of IP Addresses to Host Names.  The format is to write out your IP Address followed by a space and then the name of the host you wish to map.  For your local machine, you can use the IP Address of 127.0.0.1.  You may already see an entry written for localhost.

 

For my test, I mapped the following domains:

 

127.0.0.1         localhost

127.0.0.1         www.lewismoten.com

127.0.0.1         lewismoten.com

127.0.0.1         www.alienencyclopedia.com

127.0.0.1         alienencyclopedia.com

127.0.0.1         21stCenturyJokes.com

127.0.0.1         www.21stCenturyJokes.com

127.0.0.1         www.dummydomain.com

 

 

For this test, you will also need to enter these domains into your database.  First, I created three sites:

 

Sites

SiteID

Description

{F270E952-9DA7-46BC-A1BC-9F6171C58729}

Lewies personal website.

{957DC77D-F02B-4EF0-82C1-C94E251D2FB5}

Everything you ever wanted to know about Aliens, but were afraid to ask.

{DCFACD7A-3BBD-45A2-A89C-EF552FA952D7}

Jokes and Riddles for the 21st century.

 

Next, I created the domains to map to the sites:

Domains

DomainID

DomainName

SiteID

{E138132B-D516-46D7-9630-0BC09043FF87}

www.21stcenturyjokes.com

{DCFACD7A-3BBD-45A2-A89C-EF552FA952D7}

{B1B50AD7-8F87-45AB-A9D1-79884853970E}

www.lewismoten.com

{F270E952-9DA7-46BC-A1BC-9F6171C58729}

{5E259C7C-1404-4C53-8FD5-83074CD17043}

21stcenturyjokes.com

{DCFACD7A-3BBD-45A2-A89C-EF552FA952D7}

{01182A96-8CBD-4CDB-80DB-90EFE4F1CE32}

Localhost

{F270E952-9DA7-46BC-A1BC-9F6171C58729}

{A5B1548B-7265-4BF5-A103-B07065594865}

alienencyclopedia.com

{957DC77D-F02B-4EF0-82C1-C94E251D2FB5}

{56057427-1AC8-460D-BC74-C5F21909A22A}

lewismoten.com

{F270E952-9DA7-46BC-A1BC-9F6171C58729}

{A0E4FACD-63F9-49EB-BB17-CC9EA7674530}

www.alienencyclopedia.com

{957DC77D-F02B-4EF0-82C1-C94E251D2FB5}

 

Now – we have our domains setup in the database, and the hosts file.  Notice that I didn’t setup www.dummydomain.com.  We will use this to test domains that do not exist within the database.

 

Compile your code and visit your web page.  If you setup your project rite, your start up page should be the page with the code from this article.  You should be able to press play and go from there.

 

The page will load and let you know what the GUID is for the site that the domain is mapped to.  Try modifying the domain in your browsers address bar to the other domains in your database and see the resulting GUID change.  Now try the dummy domain and watch as the web page tells you that the domain does not exist.

 

Outcome

 

Now that you have the SiteID in your cookie, you can use this to query data from your deatabase accordingly.  For starters, try to load the site description associated with the SiteID.


Other 10 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.