views:

900

answers:

3

I have a custom site I'm building with automatic url rewriting using a custom engine. The rewriting works fine as long as the page url doesn't end in somehting like .htm or .html. For these pages it goes directly to the iis 404 page instead of hitting my rewriting engine first.

I have the * wildcard handler in the "Home Directory" section of the IIS6 configuration of that website but these urls seem to be ignored by it (altho things like css, jpg, js, etc to get sent to the url handler in my web project). How do i set up IIS6 to force these urls to get sent to the handler, while still serving the page if it exists normally?

The handler basically does this

if (!File.Exists(Request.Path))
{
    doMyRewriting();
}

I have to assume that using a block like this (just and example, the real one does some other stuff to format the Request.Path to be proper with everything) should run the "doMyRewriting()" if the requested file does not exist otherwise it will serve the page normally. Am I mistaken?

If I tell IIS specifically to send .htm and .html pages thru to the .NET handler the rewriting works but if the page is actually there it will not serve it.

Any help would be greatly appreciated.

Thanks in advance!

+1  A: 

I think if you are having IIS send all requests to .NET and your handler, then your handler will need to detect if the page exists and serve it instead of rewriting.

UrlRewriting.NET has an option to do this - you might want to look at their code to see how they're handling this case.

Jason
I just looked at UrlRewriting.NET and the actual bit where they check for if the file exists before doing any rewriting is doing the same thing as quoted above. I'm assuming(i know this is bad) that IIS is supposed to take over if the rewriter isn't triggered.
Aaron
I think this is more correct way.
Sharique
+3  A: 

Don't know if you can or would want to do this, but there is the Ionics Isapi url rewriter you can use.

http://www.codeplex.com/IIRF

Basically install that then set a rule to remove the .html that way it hits your rewrite engine. I use it on IIS 6 with several of my blogs.

percent20
This also would work if i didn't have actual .htm or .html files that needed to be served, however, that is not the case unfortunately.
Aaron
Not true. Using IIRF doesn't mean you cannot serve .htm or .html files. You can serve anything with IIRF.
Cheeso
+1  A: 

In my opinion, rewriting URLs with IIS 6 is best handled with an ISAPI filter written as unmanaged native code. Otherwise, you run into the issues you've mentioned - having to map all extensions to ASP.Net and losing the ability for simple file handling. With an ISAPI filter, you can choose to not rewrite some URLs and let IIS handle them as normal.

To get started, I suggest reading the ISAPI Filter Overview on MSDN.

If your filter absolutely needs the .Net framework runtime, it is possible to write a small ISAPI filter shell that hosts the CLR and forwards the requests to some managed code. The Filter.Net Framework takes this approach and may be suitable for your needs. There is the small drawback to this approach in that you will have to use the same .Net version as any ASP.Net applications that are run in the main IIS process.

GBegen