The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Create Unlimited Subdomains with HTTP Modules

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Brendan Tompkins

Posts: 158
Nickname: brendant
Registered: Apr, 2005

Brendan Tompkins is .NET Developer and founder of CodeBetter.Com
Create Unlimited Subdomains with HTTP Modules Posted: Jun 27, 2006 9:55 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Brendan Tompkins.
Original Post: Create Unlimited Subdomains with HTTP Modules
Feed Title: Brendan Tompkins
Feed URL: /error.htm?aspxerrorpath=/blogs/brendan.tompkins/Rss.aspx
Feed Description: Blog First. Ask Questions Later.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Brendan Tompkins
Latest Posts From Brendan Tompkins

Advertisement

I recently took the hundred or so top search words for CodeBetter.Com and created an “Explore CodeBetter” control with links to pages about those search words.  I wanted the links to be to a subdomain with the keyword as the first label of the subdomain.  So, agile would link to agile.codebetter.com, which internally would be forwarded to a handler that hooks into Community Server’s search engine and presents a list of pages containing the word agile.  As implemented, this control currently looks like this:

Setting up DNS

If your DNS supports wildcard syntax, you’ll be able to do this.   Basically you just want to forward all requests to *.yourdomain.com to yourdomain.com.  If your ISP’s dns doesn’t support the wildcard syntax, you could handle your own DNS on your server.

Setting up IIS

If you use host headers and you’ve got a bunch of different sites configured, you’ll need to add a blank host header, so that any requests for subdomains not listed  get forwarded to the proper website.   If you want to set this up for multiple websites per server, I’m not sure how to do this, perhaps someone out there knows how.  Basically the hostheader configuration won’t accept *.yourdomain.com, so I’m not sure how to set this up, except to have all un-configured domains going to one website using the blank hostheader technique.

Creating the HTTP Module

Next, you’ll need to intercept the request, inspect the requested URL and forward requests appropriately.  A good way to do this is by creating an IHttpModule and plugging it into the pipeline.  For forwarding, all you’ll need to do is handle the OnBeginRequest event, and then use Server.Transfer to forward the request.  The nice side-effect of this is that the URL in the browser doesn’t change when using Server.Transfer (as opposed to Response.Redirect), leaving your nice clean URL in the browser.  Here’s the code for my handler.  I also do some checking to make sure that the homepage (default.aspx) was requested, because I don’t want to do this on sub-page requests. 

   public class SubdomainModule : IHttpModule

    {

        public void Init(HttpApplication app)

        {

            // register for pipeline events

            app.BeginRequest += new EventHandler(this.OnBeginRequest);

        }

 

        public void Dispose() {}

 

        public void OnBeginRequest(object o, EventArgs args)

        {

 

            // get access to app and context

            HttpApplication app = (HttpApplication)o;

            HttpContext ctx = app.Context;

 

            if (!(app.Context.Request.Url.Host.StartsWith("codebetter") || app.Context.Request.Url.Host.StartsWith("www")))

            {

                if (ctx.Request.Path.ToUpper() == "/DEFAULT.ASPX")

                {

                    string[] domainParts = app.Context.Request.Url.Host.Split(".".ToCharArray());

                    if (domainParts.Length > 2)

                        ctx.Server.Transfer("/explore/explore.aspx?q=" + domainParts[0]);

                }

            }

        }

    }

Registering the Module in the pipeline

This is simple, you can just add the following to your web.config file:

<httpModules>
   <add name="SubdomainModule" type="Tompkins.Cs.Subdomains.SubdomainModule, Tompkins.Cs.SubdomainHandler" />
</httpModules>

That’s it!  Any requests will be forwarded.  You can now simply request any subdomain on codebetter, like http://monkeys.codebetter.com or http://baseball.codebetter.com.  Need advice on relationships or read that funny post about my ex-girlfriend finding my blog?  Just type http://girlfriend.codebetter.com.  Heck, you could even use us as a help system!  If you need to know about ADO, just type http://ado.codebetter.com. Good luck!

-Brendan 

 

Read: Create Unlimited Subdomains with HTTP Modules

Topic: One wrong DLL = 3 months gone Previous Topic   Next Topic Topic: One wrong DLL = 3 months gone

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use