5,696,038 members and growing! (10,822 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The Code Project Open License (CPOL)

Using HttpModules with URL Re-writing to Handle Fake Directory Requests

By Terrence Sheflin

Explains how to use HttpModule in conjunction with IIS to handle requests to fake directories
C#, C# 2.0, Windows, .NET, .NET 2.0, ASP.NET, IIS 5, IIS 5.1, IIS 6, VS2005, IIS, Visual Studio, IIS 7, Architect, Dev

Posted: 3 Apr 2007
Updated: 22 Apr 2007
Views: 25,027
Bookmarked: 33 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.68 Rating: 3.44 out of 5
0 votes, 0.0%
1
1 vote, 16.7%
2
2 votes, 33.3%
3
0 votes, 0.0%
4
3 votes, 50.0%
5

Introduction

When using HttpModules to re-write URLs, accessing a directory will not work. For example, a request to http://somewebsite/fakedirectory/ will not work unless the fakedirectory actually exists with some default index file in it. This occurs because when a request comes into IIS it first ascertains that the directory and file actually exist before ever instantiating the HttpModule in your ASP.NET application.

Solution

Solving this is actually fairly easy. The first thing to do is to load up IIS and change the 404 errors to a URL destination and point it to your site.

Screenshot - pic1.png

Screenshot - pic2.png

This will tell IIS to automatically redirect any unknown request (e.g. to a directory that does not exist) to your application. When you do this your application will receive a request like the following: http://somewebsite/default.aspx?404;http://somewebsite/fakedirectory/

Using the code

Because this is in a standard form, the HttpModule can parse this easily, extract the real website that the user wanted and then perform the standard rewrite.

internal static String GetRequestedURL(String originalRequest)
{
    // Determine the page the user is requesting.
    String url = originalRequest;

    // Check for an error URL which IIS will throw when a non-existing 
    // directory is requested and custom errors are thrown back to this site
    if (url.Contains(";") && url.Contains("404"))
    {
        // Split the URL on the semi-colon. Error requests look like:
        // errorpage.aspx?404;http://originalrequest OR
        // errorpage.aspx?404;http://originalrequest:80
        String[] splitUrl = url.Split(';');

        // Set the URL to the original request to allow processing of it.
        if (splitUrl.Length >= 1)
        {
            url = splitUrl[1];

            // Remove the first http://
            url = url.Replace("http://", "");

            // Get only the application path and beyond of the string. This 
            // may be / if it's in the root folder.
            int index = url.IndexOf(
                System.Web.HttpContext.Current.Request.ApplicationPath);
            if (index >= 0)
            {
                url = url.Substring(index);
            }
        }
    }

    return url;
}

The GetRequestURL function will take a RawURL parameter and then check if it is a 404 error. If it is, it will parse out the original request and return it. If it is not a 404 error then it will return the untouched RawURL.

Concluding Remarks

While this is a fairly simple answer, I have been looking everywhere for a solution and nobody seems to have an easy answer. Even using the HttpHandler will not solve the problem because there's no extension on the requests for a directory. This is the best solution I have found and it works every time without having to do any extensive changes.

History

April 03, 2007 - Created

April 11, 2007 - Modified to take into account root applications

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Terrence Sheflin



Occupation: Web Developer
Location: Canada Canada

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
GeneralShared HostmemberCarl Stewart15:02 6 Jun '07  
AnswerRe: Shared HostmemberTerrence Sheflin5:48 7 Jun '07  
GeneralWell donememberdorr20023:56 12 Apr '07  
GeneralRe: Well donememberAdam Tibi3:33 17 Apr '07  
GeneralHere's another related articlemembervolkan.ozcelik23:21 11 Apr '07  
GeneralRe: Here's another related articlememberzhaojicheng20:43 30 May '07  
GeneralMap *.*memberpwhe2317:02 11 Apr '07  
GeneralRe: Map *.*memberPapa Beers6:36 18 Apr '07  
GeneralNot SE friendly ! [modified]memberAlexander Turlov6:59 11 Apr '07  
GeneralRe: Not SE friendly !memberTerrence Sheflin10:53 11 Apr '07  
GeneralBrilliantmembermag3110:28 10 Apr '07  
GeneralMan this is Smart but ....memberaromr3:14 10 Apr '07  
GeneralRe: Man this is Smart but ....memberTerrence Sheflin5:27 11 Apr '07  
Generalbut the second point is importantmemberaromr5:46 11 Apr '07  
GeneralRe: but the second point is importantmemberTerrence Sheflin6:01 11 Apr '07  
AnswerRe: Man this is Smart but ....memberAlexander Turlov8:44 11 Apr '07  
GeneralHttpModule Rewrite works on fake directies under Windows XPmemberErik Loman6:07 6 Apr '07  
GeneralRe: HttpModule Rewrite works on fake directies under Windows XPmemberTerrence Sheflin5:11 9 Apr '07  

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

PermaLink | Privacy | Terms of Use
Last Updated: 22 Apr 2007
Editor: Sean Ewington
Copyright 2007 by Terrence Sheflin
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project