delphij's Chaos

选择chaos这个词是因为~~实在很难找到一个更合适的词来形容这儿了……

27 Jan 2004

URL Rewrite under Windows?

A discussion with junsu has finally result in my implementation of URL rewrite on the reverse proxy instead of IIS. He told me that there’s no official implementation of URL Rewrite and he have an ISAPI implementation several weeks ago.

According to the MSDN documentation, implementing a ISAPI filter to do URL rewrite is not so hard. I have further read MSDN and found that Microsoft.net platform has gave us another way to implement URL Rewrite, we can implement simple URL rewriting with a HttpContext.RewritePath call. The sample code is here (C#):

// Get the current HTTP request context
HttpContext myContext = HttpContext.Current;

// Rewrite it with another URL
myContext.RewritePath(“HttpContext_Next.aspx”);

With the regular expression Microsoft .net Framework supplied, we can do further. It’s not too hard (could be done within less than 40 lines of code, I guess) to implement a URL rewrite using ASP.net technology and can be easily configured through web.config (by implementing our own version of IConfigurationSectionHandler, which is supposed to support web.config style configuration and that’s preferred because this is more consistent with the other parts of the site), and some hack in global.asa (this is to catch all requests).

The downsides of this implementation is that it’s required to modify sitewise (or application-wise) Application Mapping to map different file extension names to ASP.net’s aspnet_isapi.dll filter. This will make it harder to deploy the application, and what’s more, might pose a security vulnerablity if configurated incorrectly. What’s more, this approach does not support non-.net application, a.k.a. legacy application like ASP scripts :-(

Any ideas? I think this is really useful.