Introduction
In this article I describe how to use a custom HttpHandler
as a domain-forwarder.
Background
With the Hostheader feature in IIS you can map many hostnames to one IP-Address. For example your IIS Server has the IP-Address 195.243.118.165. You or your ISP has some A-Records for this IP-Address.
Now, you have configured a website in IIS which responds to all these domains. The following image show a sample IIS configuration.
All requests to www.mydomain.com, www.virtualdomain1.com and www.virtualdomain2.com target the same website. Often you have to do such configurations because the amount of public IP-addresses is limited.
At this point you want to provide the user a different webpage depending on the used domain name. This sample application will provide a simple solution for this problem.
Using the code
Create a class in your ASP.NET application which implements the IHttpHandler
. You have to implement one method and one property:
public void ProcessRequest (HttpContext)
public void IsReusable
Here is the sample code:
public class DomainHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
String redir = ConfigurationSettings.AppSettings
[context.Request.Url.Host];
if (redir != null)
{
if (RedirectIsValid(redir, context.Request.Url))
{
context.Response.Redirect(redir, true);
}
else
{
context.Response.Write("<h1><font
color=red>Error invalid DomainHandler
configuration</font></h1><br>
<b>Please check your Web.config
file.</b>");
}
}
}
private bool RedirectIsValid(String redir, Uri currentUri)
{
String val1 = redir.ToLower();
String url = currentUri.AbsoluteUri.ToLower();
String host = currentUri.Host.ToLower();
if (val1 == url) { return false; }
if (val1 == ( "http://" + host)) { return false; }
if (val1 == ("http://" + host + "/")) { return false; }
if (val1 == host) { return false; }
if (val1 == (host + "/")) { return false; }
return true;
}
public bool IsReusable
{
get
{
return true;
}
}
}
Points of interest
I decided to store my URL mappings in the web.config file as key-value pairs in the appSettings
Section. Additionally you must register the new HttpHandler
in your web.config file.
Sample web.config file is provided here:
="1.0" ="utf-8"
<configuration>
<appSettings>
-->
<add key="localhost" value="http://localhost/DomainForward/target1.htm" />
<add key="www.virualdomain1.com" value="http://www.myDomain.com/domain1" />
<add key="www.virualdomain2.com" value="http://www.myDomain.com/domain2" />
</appSettings>
<system.web>
<compilation
defaultLanguage="c#"
debug="true"
/>
<httpHandlers>
<add verb="*" path="Default.aspx"
type="DomainFilter.DomainHandler, DomainFilter" />
</httpHandlers>
</system.web>
</configuration>
In the path
attribute in the httpHandlers
section you specify a really existing filename which matches your "IIS-Defaultpage-Configuration".
In my sample application for simplicity I used a subweb, the default in Visual Studio. In a real world example, you would install the application in the root-web.