Click here to Skip to main content
13,250,043 members (68,806 online)
Click here to Skip to main content
Add your own
alternative version


67 bookmarked
Posted 11 Mar 2003

Domain Forwarding with IHttpHandler

, 9 Sep 2003
Rate this:
Please Sign up or sign in to vote.
Use HttpHandlers to forward the user to different webpages depending on the requested hostname (domainname).


In this article I describe how to use a custom HttpHandler as a domain-forwarder.


With the Hostheader feature in IIS you can map many hostnames to one IP-Address. For example your IIS Server has the IP-Address 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, and 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
        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);
                // display an error.
                   color=red>Error invalid DomainHandler 
                   <b>Please check your Web.config 

    // 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
            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" ?>
<!--<span class="code-comment">
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="" value="" />
<add key="" value="" />






<add verb="*" path="Default.aspx" 

    type="DomainFilter.DomainHandler, DomainFilter" />



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.


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
Web Developer
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionBind Domain to Application Pages Pin
Morteza M17-Nov-12 5:05
groupMorteza M17-Nov-12 5:05 
QuestionBlank page Pin
thanks_in_advance13-Sep-12 0:57
memberthanks_in_advance13-Sep-12 0:57 
GeneralForwarding IP Pin
Om Rudi29-Mar-09 7:38
memberOm Rudi29-Mar-09 7:38 
GeneralSmall improvement to RedirectIsValid Pin
Sameer Alibhai31-Jul-07 11:19
memberSameer Alibhai31-Jul-07 11:19 
GeneralDifference on IIS6 [modified] Pin
spdl200027-Oct-06 14:27
memberspdl200027-Oct-06 14:27 
GeneralMultiple URL Same Website Pin
mukeshdas4-Oct-04 1:06
membermukeshdas4-Oct-04 1:06 
GeneralRe: Multiple URL Same Website Pin
Andi Fleischmann4-Oct-04 1:39
memberAndi Fleischmann4-Oct-04 1:39 
You have to choose depending on your access permission on the webserver.
If your are lucky to be in full control of the webserver you can use either. The more obvious is to use the webserver's own tools (Hostheader, Virtual Directory, Redirect to URL, etc.)

If not, you have to a workaround using some code, for example the this little domain forwarding.

Secondly it depends on the configuration options and capabilities of your estore. I do not know any details.

When using the code in this article:

  1. Download the code
  2. Build it
  3. Copy the resulting dll in your bin directory
  4. Add your hostnames and the target directories to the web.config file as show in the example.
  5. Additionally configure the httpHandlers section in the web.config file

GeneralRe: Multiple URL Same Website Pin
mukeshdas5-Oct-04 21:00
membermukeshdas5-Oct-04 21:00 
GeneralRe: Multiple URL Same Website Pin
Andi Fleischmann5-Oct-04 23:46
memberAndi Fleischmann5-Oct-04 23:46 
GeneralVB.NET Pin
jtisdale25-Sep-04 13:10
sussjtisdale25-Sep-04 13:10 
GeneralRe: VB.NET Pin
Dicke Berta30-Sep-04 5:34
memberDicke Berta30-Sep-04 5:34 
GeneralRe: VB.NET Pin
jtisdale30-Sep-04 11:21
memberjtisdale30-Sep-04 11:21 
GeneralMapping .html/.htm to .aspx Pin
Neal Stublen11-Sep-03 21:55
memberNeal Stublen11-Sep-03 21:55 
GeneralRe: Mapping .html/.htm to .aspx Pin
Neal Stublen11-Sep-03 22:28
memberNeal Stublen11-Sep-03 22:28 
Questionwhats the point? Pin
Philip Fitzsimons11-Sep-03 3:52
memberPhilip Fitzsimons11-Sep-03 3:52 
AnswerRe: whats the point? Pin
Andi Fleischmann11-Sep-03 6:59
memberAndi Fleischmann11-Sep-03 6:59 
Questionhow about server.execute ? Pin
Becio18-Mar-03 0:00
memberBecio18-Mar-03 0:00 
AnswerRe: how about server.execute ? Pin
Blake Coverett10-Sep-03 13:13
memberBlake Coverett10-Sep-03 13:13 
GeneralApp inside App Pin
J.M.Piulachs14-Mar-03 0:19
memberJ.M.Piulachs14-Mar-03 0:19 
GeneralRe: App inside App Pin
Da Bert14-Mar-03 19:01
memberDa Bert14-Mar-03 19:01 
GeneralWhat if no file name? (was &quot;App inside App&quot;) Pin
Rhy Mednick19-Sep-03 7:53
memberRhy Mednick19-Sep-03 7:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 10 Sep 2003
Article Copyright 2003 by Andi Fleischmann
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid