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.