By Wyatt Barnett

Making IIS 6.0 Play With UrlRewriting

By Wyatt Barnett

One topic that comes up often in SitePoint’s .NET Forum is “How do I make rewrite Urls in .NET?” The answer to this question is relatively simple, once you get into the ASP.NET pipeline. Unfortunately, given the current versions of IIS, getting to that pipeline is alot trickier than it should be. IIS really prefers to deal with known extensions rather than fancy mappings. Documentation for getting around this limitation is sparse at best.

Getting IIS Mapping Things it Does Not Want To Map

First and foremost among IIS 7’s new features is the ability to inject HttpHandlers and HttpModules on any request coming to the server. There is a reason this is the headliner—getting IIS 6 to correctly map rewritten urls is a trick in and of itself. IIS 6.0 and earlier really, really want to just take the extension from the request and map it to the proper handler for processing. So, if you wish to make your urls something like you are going to have to do a bit of engineering. Options include:

  1. Let IIS do what IIS wants to do.

    There are times when it is best to kowtow to how the underlying technology wants to work. So just change your pages to feel like pages rather than folders—such as The url is nearly as hackable as the previous example while needing considerably less engineering. Your application will require no reconfiguration on the IIS level to get things mapping correctly. This tactic will also work in shared hosting scenarios where you don’t have the ability to make significant configuration changes in IIS.

  2. Use an ISAPI Module or Filter

    ISAPI modules and filters sit within IIS and handle requests before they hit ASP.NET’s pipeline. The two options are the rather cheap IISRewrite and the free ISAPI Rewrite Filter. Both use a grammar generally reminiscent, but not identical to, of mod_rewrite. This is probably the cleanest solution, but it also requires a hosting situation that lets one make significant configuration changes to IIS.

  3. Use a Wildcard mapping.

    IIS 6.0 supports wildcard mappings, where all requests to a given website are mapped to a given handler. While this sounds like a silver bullet, it has a lot of downsides. Probably the biggest one is that requests for static resources are going to be patched through the ASP.NET pipeline. Moreover, if you are using any estoric content types, you will have to make sure they are assigned a proper handler in your application or in the server’s web.config.

  4. Use a custom ASP.NET 404 page.

    If your 404 page is an ASP.NET page, then it will run the entire rendering pipeline, including calling your custom HttpModules. So, any non-existent file will still let your program get a few words in. While comparatively rather ghetto, this is a valid tactic for situations where one cannot modify significant bits of IIS nor just use .aspx urls. Unfortunately, most shared hosts will not let one use options two or three above, so this lowest common denominator is quite often used in applications today.

Now that we have the hard part—getting IIS to feed requests to ASP.NET—taken care of, we can get to the business of actually rewriting URLs. Fortunately, this is relatively easy and markedly more sane than the above. Rather than repeat the wisdom of the interwebs, I shall give you some key links:

  1. Scott Guthrie’s excellent post on Url Rewriting in .NET. Pay special attention to the last few points; they cover a few of the big gotchas in .NET url rewriting.
  2. UrlRewritingNet.UrlRewrite is a rather good .NET 2.0 library for handling the mechanics of your url rewriting needs.
  3. UrlRewriter.Net is another rather good, open source .NET 1.1/2.0 library for handling the mechanics of your url rewriting needs.
  4. Scott Watermasysk, the man who brought you .TEXT and Community Server, posts this very helpful post on a rather significant bug in .NET’s native url rewriting.
  5. The MSDN documentation for HttpContext.RewritePath, the “key” method in this endeavor.

So, take control of IIS and get to making them urls more human friendly!

kick it on

  • McKenna

    I use Helicon’s ISAPI Rewrite and have found it to be the easiest module to use for IIS URL rewriting. The latest version is fully .htaccess compatible which makes it a whole lot easier if you are moving from Apache.

  • Anonymous

    > Both use a grammar generally reminiscent, but not identical to, of
    > mod_rewrite.

    Of course not; But there is a lot more that you can do with Apache than purely modify the url… I wonder if IIS7 will be as capable as Apache once launched?

    I doubt it. Apache is a beautiful piece of software engineering which still continues to surprise and embolden us developers, and IIS from Microsoft proves nothing but a hinderance.

    Skeptic? Sure…

    Dr Livingston

  • I use Apache and mod_rewrite for rewriting urls…

    oh, no support for websites you say?

    mod_aspnet my friend!

  • sorry, my bad … mod_aspdotnet

    used to be updated by apache, but now on sourceforge

  • I’m usin a custom solution by using the global.asax for every Request being made.

    I’m really looking forward to the integrated IIS7 rewrite features but I assume it gonna take some time until the hosting companies offer w2k8 hosting..

  • Apache Killer

    Dr Livingston – As much as I love rhetorical ponderings from Apache lovers who live in their parents basement, I don’t.

    Why is it that only Apache devotees feel the need to jump into a completely unrelated message thread and bash entire systems. Maybe you are just sore because IIS outserves Apache in the top Fortune 1000 companies??

    Oh, right.. they must all be idiots, running those Fortune 1000 systems.. purely bloatware dictated by morons in middle management. If they were really 3l1t3 they’d be running Apache on a x386 powered by a potato.

    PS. This is the ultimate IIS vs Apache snarky message thread post. There is no comeback. Simply find a shower, sit in it, and cry.

  • irrevelent linux

    Amen Apache Killer. Stop drinking the kool-aid, and think for yourself Dr.. Stats dont lie player, if you want to made considerate money, Microsoft is the place to be. Just go ask the PHP developers in the unemployment line.

  • DirectAttack

    It’s not about technology is about how you use it because nor Apache nor IIS are complete pieces of c*ap. It all comes down to the preference of the decision maker or a company that puts the product on the market.

    At the end nothing FREE is ever completely free. Selling software or services or however you wanna call it is one the same thing. Programmers, salesmen, marketing people… they all have to eat.

  • MikeSchinkel

    For those looking for a resource on URL design and rewriting check out

    That said, IIS 6.0 is a very frustrating beast having given so little consideration to URL rewriting, and Apache is a very frustrating beast in that one tiny misplaced character in the httpd.conf file can bring the server crashing down rather than running but cleanly reporting the error. In reality both have significant strengths and both have significant failings.

  • Don’t worry guys, IIS7 will have pretty good URL Rewriting support.

  • wwb_99

    @MikeSchinkel: thanks, that looks like a handy resource. And yes, you hit the nail on the head–both Apache and IIS have their place in this world and both are viable HTTP application servers with their individual strengths and weaknesses.

    I do, however, thin IIS7 could tip the balance towards IIS. It manages to cover IIS’ principal weaknesses–url rewriting, non-administrative configuration via text (well, XML) files.

  • MikeSchinkel

    @wwb_99: I have both IIS7 and Apache 2.2 on my Vista laptop. I’ve tried IIS7 and, while I would have killed for it’s feature set back in 1998/2000 today it is too little too late. And too much damage has been done from IIS6’s lack of URL rewriting. IIS7 is too cumbersome; it’s a grand design, which is typical Microsoft, but it would be better were it less elaborate.URL rewriting requires me to write a module in C#? Come on, as everything Redmond, it is over-engineered and makes things harder than they should be. And this is from a former staunch Microsoft technologies advocate. Today I’m using Apache and happily not looking back.

  • wwb_99

    I think you missed a key point–IIS7 lets you use .NET HttpModules on any web application, and the HttpModules that do URL rewriting already exist. I have also heard that Microsoft will be releasing their own implementation.

    A key thing to remember is that IIS7 really is not quite out yet, even if it is included with Vista. The real-deal version is part of Windows Server 2008, so I suspect the other real web hosting tools will come then.

  • MikeSchinkel

    @wwb_99: I didn’t exactly miss your point regarding IIS7, I just didn’t find it relevant. The fact that Microsoft has not delivered an out-of-the-box strategy for URL rewriting is telling. It shows how little importance they place on URL rewriting whereas to me URL rewriting is one of the most important functionalities a web server can offer. (BTW, though Apache offers URL rewriting out-of-the-box (with the mod_rewrite module) Apache’s use of error-prone regular expressions for of URL rewriting is painful at best, dangerous at worst. I look forward to a day when we can use URI Templates for URL Rewriting.)

    That said, you made my point about IIS7 being too-little, too-late by saying:

    “A key thing to remember is that IIS7 really is not quite out yet, even if it is included with Vista.”

    In 2000 I would have been really excited about IIS7 shipping on Windows 2000 Server. Today I could care less as it’s clear Microsoft’s development cycles are no longer meeting the needs of web developers in ways that the open-source community is.

  • Mahdi Taghizadeh

    Wyatt, thank you for your comment on SharpLife.NET. I updated my post with a reference to your article.

  • Daren

    4. Scott Watermasysk, the man who brought you .TEXT and Community Server, posts this very helpful post on a rather significant bug in .NET’s native url rewriting.

    This line had two generic links but not a link to the actual post where Scott talks about the bug.

Get the latest in Front-end, once a week, for free.