Click here to Skip to main content
15,867,488 members
Articles / Web Development / HTML

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

Rate me:
Please Sign up or sign in to vote.
4.47/5 (18 votes)
30 Oct 2014CPOL1 min read 95.4K   70   13
Explains how to use HttpModule in conjunction with IIS to handle requests to fake directories

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.

httpmoduledirectoryhandle/pic1.png

Solution

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

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.

C#
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)


Written By
Team Leader
Canada Canada
I am a Lead Software Architect (UI/UX), focusing mainly on developing with JavaScript, C#, ASP.NET WebApi and MVC.

Comments and Discussions

 
Questionhave you consider to post this as a tip? Pin
Nelek8-Nov-14 13:25
protectorNelek8-Nov-14 13:25 
QuestionMy vote of 5! Pin
Volynsky Alex4-Nov-14 8:43
professionalVolynsky Alex4-Nov-14 8:43 
QuestionHijacked? Pin
Chuck141128-Feb-11 8:18
Chuck141128-Feb-11 8:18 
General[My vote of 1] This article is absolutely wrong Pin
gaurav_verma_mca6-Nov-10 6:36
gaurav_verma_mca6-Nov-10 6:36 
GeneralRe: [My vote of 1] This article is absolutely wrong Pin
Terrence Sheflin8-Nov-10 4:35
Terrence Sheflin8-Nov-10 4:35 
GeneralRe: [My vote of 1] This article is absolutely wrong Pin
gaurav_verma_mca8-Nov-10 5:13
gaurav_verma_mca8-Nov-10 5:13 
GeneralRe: [My vote of 1] This article is absolutely wrong Pin
Terrence Sheflin8-Nov-10 5:15
Terrence Sheflin8-Nov-10 5:15 
It's not a debate, you didn't try it and, with no proof or indication that you tested it on its targeted platform, deemed it not working. Then you provided a link to a video, that you didn't make, with a solution for a much newer version of IIS.

I can say your comment is wrong too, with absolutely no proof, or trial.
GeneralRe: [My vote of 1] This article is absolutely wrong Pin
gaurav_verma_mca8-Nov-10 5:16
gaurav_verma_mca8-Nov-10 5:16 
GeneralMy vote of 1 Pin
gaurav_verma_mca6-Nov-10 6:35
gaurav_verma_mca6-Nov-10 6:35 
GeneralRe: My vote of 1 Pin
Terrence Sheflin8-Nov-10 4:35
Terrence Sheflin8-Nov-10 4:35 
GeneralRe: My vote of 1 Pin
gaurav_verma_mca8-Nov-10 5:12
gaurav_verma_mca8-Nov-10 5:12 
GeneralRe: My vote of 1 Pin
Terrence Sheflin8-Nov-10 5:13
Terrence Sheflin8-Nov-10 5:13 
GeneralRe: My vote of 1 Pin
gaurav_verma_mca8-Nov-10 5:13
gaurav_verma_mca8-Nov-10 5:13 

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.