Brendan Tompkins [MVP]

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Create Unlimited Subdomains with HTTP Modules

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 

 


Posted 06-27-2006 11:15 AM by Brendan Tompkins

[Advertisement]

Comments

Steve wrote re: Create Unlimited Subdomains with HTTP Modules
on 06-27-2006 12:32 PM
You can't imagine how many times I've wondered what the best way to find all the posts about monkeys was on the codebetter.com site! :)
Brendan Tompkins wrote re: Create Unlimited Subdomains with HTTP Modules
on 06-27-2006 1:20 PM
Steve,  yes, I've often wondered the same...  :) I'm actually quite suprised at the number of monkey posts!
Community Server Daily News wrote Community Server Daily News for Tuesday, June 27, 2006
on 06-27-2006 2:47 PM
Community Server featured in a Forrester Wave™ Vendor Summary with the title, &quot;Telligent Provides A Strong,
dotnetkicks.com wrote Create Unlimited Subdomains with HTTP Modules
on 06-27-2006 4:33 PM
Trackback from dotnetkicks.com
Jason Haley wrote Interesting Finds: June 27, 2006 PM edition
on 06-27-2006 5:11 PM
Tiernans Comms Closet wrote Create unlimited subdomains with HTTP Modules
on 06-28-2006 7:44 AM
Now, this is an interesting idea: create unlimited sub domains using HTTP modules on .NET. This could...
Jeffrey Palermo wrote re: Create Unlimited Subdomains with HTTP Modules
on 06-28-2006 10:38 AM
Vince wrote re: Create Unlimited Subdomains with HTTP Modules
on 06-28-2006 1:43 PM
Just wondering...
why did you choose to compare strings using:
(ctx.Request.Path.ToUpper() == "/DEFAULT.ASPX")

Would a case-insensitive compiled Regex.IsMatch() be faster/better?
Brendan Tompkins wrote re: Create Unlimited Subdomains with HTTP Modules
on 06-28-2006 1:56 PM
Vince,

I think you're right.. I've actually gone back into this code to refactor for stuff like this.. (Theres some embarrasing mix of local variable ctx versus using the context instance stuff going on too)...  

I'm usually not a big perf guy, but with this code, since it's running on every request, I'd error on the side of performance,  which is a round about way of saying you're right, but I usually use string comparisons for readability.

Thanks!  
Soheilkh wrote re: Create Unlimited Subdomains with HTTP Modules
on 06-29-2006 10:49 AM
Hi
is maybe that map the subdomain in localhost.
i can't test your sample code .when i request subdomain in addressbar SubdomainModule can't hanlde request.
how i do?
plz help me
Brendan Tompkins wrote re: Create Unlimited Subdomains with HTTP Modules
on 06-29-2006 10:54 AM
Soheikh, have you setup your wildcard DNS to point to your website?  
Aaron Erickson wrote re: Create Unlimited Subdomains with HTTP Modules
on 06-29-2006 11:55 AM
Great stuff!  Of course, this is a great demo of what, in general, you can do with HttpModules.  I already wrote my own module that allows me to map a database that has landing URLs (for consumption by google) - to the appropriate querystringed URL via a 301 redirect - using your module as inspiration... brilliant!
Technical Related Notes » Blog Archive » links for 2006-06-29 wrote Technical Related Notes &raquo; Blog Archive &raquo; links for 2006-06-29
on 06-29-2006 10:57 PM
soheilkh wrote re: Create Unlimited Subdomains with HTTP Modules
on 06-30-2006 6:48 AM
i can't access to dns setup .
is way that map all subdomain in iis setup
Brendan Tompkins wrote re: Create Unlimited Subdomains with HTTP Modules
on 06-30-2006 9:04 AM
Soheikh,

Nope. You've gotta first get the traffic to your server, and you have to do that through DNS.
Firoz Ansari » Blog Archive » Create Unlimited Subdomains with HTTP Modules wrote Firoz Ansari &raquo; Blog Archive &raquo; Create Unlimited Subdomains with HTTP Modules
on 07-01-2006 9:58 AM
soheilkh wrote re: Create Unlimited Subdomains with HTTP Modules
on 07-01-2006 5:22 PM
tanks btompkins .
Sahil Malik wrote re: Create Unlimited Subdomains with HTTP Modules
on 07-11-2006 7:10 AM
The words "sahil" and "agile" are together. Is there a hidden message here?
jim wrote re: Create Unlimited Subdomains with HTTP Modules
on 08-11-2006 3:35 AM
when i request this page with url http://aaa.cc.codebetter.com/blogs/brendan.tompkins/archive/2006/06/27/146875.aspx . but it also open the same page , let us look at this code :
if (domainParts.Length > 2)

                       ctx.Server.Transfer("/explore/explore.aspx?q=" + domainParts[0]);
from this code ,the programe should open an other page ,but it is not ,why ?
Brendan wrote re: Create Unlimited Subdomains with HTTP Modules
on 08-11-2006 9:41 AM
Jim, I think you're missing this line

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

Any page request will be ignored..
sub Domain « ali raza wrote sub Domain &laquo; ali raza
on 11-03-2006 12:46 PM
Daily News Faq List wrote How to create unlimited subdomains with HTTP Modules in Community Server
on 11-22-2006 10:51 AM

Brendan Tompkins demonstrates how to create unlimited subdomains with HTTP Modules in Community Server

VinayC wrote re: Create Unlimited Subdomains with HTTP Modules
on 02-19-2007 11:29 PM

Wouldn't it work only if requesting url is for aspx page or some other asp.net resource? For other resources (such as html and images), IIS will try to handle the request; so asp.net runtime won't even get invoked.

Community Server Bits wrote How to create unlimited subdomains with HTTP Modules in Community Server
on 03-12-2007 11:03 AM

Brendan Tompkins demonstrates how to create unlimited subdomains with HTTP Modules in Community Server

Pradeep wrote re: Create Unlimited Subdomains with HTTP Modules
on 07-05-2007 6:07 AM

thank you sir for this valuble information

salman aa wrote re: Create Unlimited Subdomains with HTTP Modules
on 07-16-2007 11:14 PM

Thanks a lot.

Paul wrote re: Create Unlimited Subdomains with HTTP Modules
on 08-19-2007 4:25 PM

I am wondering how I can get the traffic to my server through the DNS. Can you please lead me to links for instructions how to do this? Also I didn't understand the point of the blank hostheader, what does this mean, any instructions about doing this?

Abishek.R.Srikaanth wrote re: Create Unlimited Subdomains with HTTP Modules
on 11-28-2007 2:48 AM
What happens to the existing subdomains which are configured in the control panel, say the subdomains which are configured for mail and the controlpanel using subdomains
mohsen wrote re: Create Unlimited Subdomains with HTTP Modules
on 03-01-2008 10:44 AM

thanks a lot but i have a question:

when i want to run my project and test this technique an error appear:

Could not load type 'SubdomainModule' from assembly 'vsub'

my assembly name is vsub and my class is SubdomainModule

can you say me what should i do for solve it

http://girlfriend.codebetter.com/blogs/brendan.tompkins/archive/2006/06/27/146875.aspx wrote http://girlfriend.codebetter.com/blogs/brendan.tompkins/archive/2006/06/27/146875.aspx
on 03-25-2008 4:27 AM
Govindaraj wrote re: Create Unlimited Subdomains with HTTP Modules
on 03-28-2008 3:29 AM

Hai,

  What you have type inside the type parameter in the web.config file.

jockeyvn wrote re: Create Unlimited Subdomains with HTTP Modules
on 03-29-2008 11:37 AM

Dear all,

Please help me, I cannot find subdomainhandler in cs file of Brendan Tompkins .

And parameter in Web.config make me confuse.

Please explain clearly.

Thanks in advance

Mojtaba wrote re: Create Unlimited Subdomains with HTTP Modules
on 05-22-2008 3:41 AM

thanks i used it in my digital library

http://ent.ut.ac.ir/lib

manish wrote re: Create Unlimited Subdomains with HTTP Modules
on 05-22-2008 8:28 AM

I am getting error while adding this :

<httpModules>

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

</httpModules> to my web config please help .

Ahmed wrote re: Create Unlimited Subdomains with HTTP Modules
on 06-22-2008 9:19 AM

Why when i ues subdomain Module my session lost

how to creat a subdomain wrote how to creat a subdomain
on 07-03-2008 12:59 AM

Pingback from  how to creat a subdomain

Create SubDomain and FormAuthentication « Jwalin’s Weblog wrote Create SubDomain and FormAuthentication &laquo; Jwalin&#8217;s Weblog
on 07-21-2008 4:48 PM

Pingback from  Create SubDomain and FormAuthentication &laquo; Jwalin&#8217;s Weblog

Izadpour wrote re: Create Unlimited Subdomains with HTTP Modules
on 09-13-2008 3:54 AM

so useful

Adhok wrote re: Create Unlimited Subdomains with HTTP Modules
on 11-01-2008 7:57 AM

Good. But im confused

Thomas wrote re: Create Unlimited Subdomains with HTTP Modules
on 12-15-2008 2:18 PM

I need to find a way to treat all subdomain urls coming into my domain as the actual path....

My controls and App Code folder are not being recognized when the url uis entered as a subdomain - any ideas>>>

Muthukrishnan wrote re: Create Unlimited Subdomains with HTTP Modules
on 01-02-2009 2:01 AM

Could not load file or assembly 'SubdomainModule.domain.SubdomainHandler' or one of its dependencies. The system cannot find the file specified. (F:\Chandru\SubdomainModule\web.config line 17)

The error occured. wat wil do?

MePresidentObamaCan.CodeBetter.com wrote re: Create Unlimited Subdomains with HTTP Modules
on 02-08-2009 10:51 AM

Even Pres Obama can code better than you Mr. Brendan Tompkins.

jaiganesh wrote re: Create Unlimited Subdomains with HTTP Modules
on 02-12-2009 2:03 AM

hi..

 in local host,i

i have problem in web.config file(httpmodule) and also i want to know how to assign the httpapplication object.i am very new in .net.so pls tell me

dotnetguts wrote re: Create Unlimited Subdomains with HTTP Modules
on 02-19-2009 2:40 AM

Thanks it was helpful.  A quick question, when we are using UrlRewriter will it create any problem?

Diego Marques wrote re: Create Unlimited Subdomains with HTTP Modules
on 02-26-2009 4:01 PM
Create Unlimited Subdomains with HTTP Modules « Jwalin Khatri wrote Create Unlimited Subdomains with HTTP Modules &laquo; Jwalin Khatri
on 02-27-2009 3:41 PM

Pingback from  Create Unlimited Subdomains with HTTP Modules &laquo; Jwalin Khatri

Ozzie Perez » Unlimited SEO with Subdomains using HttpModules wrote Ozzie Perez &raquo; Unlimited SEO with Subdomains using HttpModules
on 03-10-2009 5:17 PM

Pingback from  Ozzie Perez &raquo; Unlimited SEO with Subdomains using HttpModules

Add a Comment

(required)  
(optional)
(required)  
Remember Me?