Sub-Domains And Shared Hosting

Overview

Sub-domains have several uses. For a very large site they can be used to load balance their servers and they can be used for branding, e.g., news.yahoo.com, finance.yahoo.com, etc. "Friendly" URLs (sweaters.Fleece-me.com instead of www.Fleece-me.com/prods/sweaters.aspx) are also a common use.

If you are running a small to medium sized site in a hosted environment, sub-domains can still be used. Managing sub-domains, mapping them to an IP address or a sub-folder on your site, and URL rewriting can come into play for this convenience, however.

If you are hosting your site in a shared environment, hopefully you have the ability to add sub-domains via your Admin screens. But the hoster may charge you for any additional customizations.

URL Rewriting

URL rewriting comes into play because many times the friendly URL is really just a logical name for a physical resource, as in the sweaters.Fleece-me.com example above.

There are a variety of ways to accomplish this functionality, but perhaps no single "one solution fits all", particularly in a shared hosting environment where you will have limited control, or will be charged for each modification requested.

Some choices are:

Each has their pros and cons, and I highly recommend Scott Mitchell's article, which goes into some detail regarding the Request life-cycle, authentication type, "gotchas", and so forth. Some solutions require changes to the Global_asax handler, some to config files, or require developing modules or handlers. Some require issuing a context.RewritePath, Redirect or Transfer. Some are sensitive to the type of authentication being used.

Sub-Domain / Shared Hosting Example

If your website is www.dough-nuts.com, normally the hoster will also give you one sub-doman, dough-nuts.com, which is mapped to www.dough-nuts.com. The user can type in either and will be directed to your website.

But, being the enterprising marketer that you are, you want to have more choices:

Each of these would be a sub-domain of www.dough-nuts.com, and each would take the user to specific areas of the website that focuses on a subset of all doughnuts.

For this article, we will focus on a website with the following constraints/design choices:

Our solution will do the following:

That's pretty much it! No Global_asax changes, HTTP Handlers or Modules, or 3rd party DLLs.

The following code would be required (such as in the Page_Load) for each page that you wish to customize based on the sub-domain:

'----------------------------------------------------------
' Based on sub-domain, load appropriate User Control
'----------------------------------------------------------
  Dim sCurrentPath As String = Request.ServerVariables("HTTP_HOST")

  If sCurrentPath.Contains("jelly.dough-nuts.com") Then
    Dim ctl As jellyTitle
    ctl = CType(LoadControl("~/Controls/jellyTitle.ascx"), jellyTitle)
    PlaceHolder1.Controls.Add(ctl)
  ElseIf sCurrentPath.Contains("glazed.dough-nuts.com") Then
    Dim ctl As glazedTitle
    ctl = CType(LoadControl("~/Controls/glazedTitle.ascx"), glazedTitle)
    PlaceHolder1.Controls.Add(ctl)

    ...

  End If

A couple of side notes:

Summary

This technique is a simple way to customize a reasonable number of pages based on sub-domains in a shared hosting environment, where your control may be limited and/or site modifications by the hoster may become costly. Cheers!

Steve