Click here to Skip to main content
6,630,586 members and growing! (17,566 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Domain Forwarding with IHttpHandler

By Andi Fleischmann

Use HttpHandlers to forward the user to different webpages depending on the requested hostname (domainname).
C#, Windows, .NET 1.0, ASP.NET, Visual Studio, Dev
Posted:11 Mar 2003
Updated:9 Sep 2003
Views:105,111
Bookmarked:59 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 3.73 Rating: 3.73 out of 5
3 votes, 30.0%
1

2

3
3 votes, 30.0%
4
4 votes, 40.0%
5

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.

www.mydomain.com A 195.243.118.165
www.virtualdomain1.com A 195.243.118.165
www.virtualdomain2.com A 195.243.118.165

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
{
    /// Implementing IHttpHandler

    public void ProcessRequest(HttpContext context) 
    {
        // Extract the host name from the request url

        // an look up the redirect url in the Web.config

        String redir = ConfigurationSettings.AppSettings
                              [context.Request.Url.Host];
        if (redir != null)
        {
            // check for valid redirect url, preventing loops

            if (RedirectIsValid(redir, context.Request.Url))
            {
                // redirect the user to the url found in web.config

                context.Response.Redirect(redir, true);
            }
            else
            {
                // display an error.

                context.Response.Write("<h1><font 
                   color=red>Error invalid DomainHandler 
                   configuration</font></h1><br>
                   <b>Please check your Web.config 
                   file.</b>");
            }
        }
    }

    // prevents possible redirect loops

    // it is not allowed to have an redirect url targeting its self

    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;
    }

    /// Implementing IHttpHandler

    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:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!--
Use as keys your full qualified host names.
As Value you provide a absolute url, you want to redirect to.
--> 
<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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Andi Fleischmann


Member

Occupation: Web Developer
Location: Germany Germany

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralForwarding IP PinmemberOm Rudi7:38 29 Mar '09  
GeneralSmall improvement to RedirectIsValid PinmemberSameer Alibhai11:19 31 Jul '07  
GeneralDifference on IIS6 [modified] Pinmemberspdl200014:27 27 Oct '06  
GeneralMultiple URL Same Website Pinmembermukeshdas1:06 4 Oct '04  
GeneralRe: Multiple URL Same Website PinmemberAndi Fleischmann1:39 4 Oct '04  
GeneralRe: Multiple URL Same Website Pinmembermukeshdas21:00 5 Oct '04  
GeneralRe: Multiple URL Same Website PinmemberAndi Fleischmann23:46 5 Oct '04  
GeneralVB.NET Pinsussjtisdale13:10 25 Sep '04  
GeneralRe: VB.NET PinmemberDicke Berta5:34 30 Sep '04  
GeneralRe: VB.NET Pinmemberjtisdale11:21 30 Sep '04  
GeneralMapping .html/.htm to .aspx PinmemberNeal Stublen21:55 11 Sep '03  
GeneralRe: Mapping .html/.htm to .aspx PinmemberNeal Stublen22:28 11 Sep '03  
Generalwhats the point? PinmemberPhilip Fitzsimons3:52 11 Sep '03  
GeneralRe: whats the point? PinmemberAndi Fleischmann6:59 11 Sep '03  
Generalhow about server.execute ? PinmemberBecio0:00 18 Mar '03  
GeneralRe: how about server.execute ? PinmemberBlake Coverett13:13 10 Sep '03  
GeneralApp inside App PinmemberJ.M.Piulachs0:19 14 Mar '03  
GeneralRe: App inside App PinmemberDa Bert19:01 14 Mar '03  
GeneralWhat if no file name? (was "App inside App") PinmemberRhy Mednick7:53 19 Sep '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Sep 2003
Editor: Smitha Vijayan
Copyright 2003 by Andi Fleischmann
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project