ASP.NET URL rewrites using .htaccess-like syntax

Edit on GitHub

Having a PHP background, I've been using .htaccess mod_rewrite in Apache for ages. ASP.NET allows rewriting too, but using a different syntax than mod_rewrite. Using the attached library, you can now use mod_rewrite syntax to perform rewrites on your ASP.NET application. Here's how...

First of all, you need to download the attached library. Reference it from your web project, and register it as a module in Web.config, preferrably as the first one:


    <add name="UrlRewriter" type="MaartenBalliauw.UrlRewriter.Rewriter"/>
    <!-- Other modules can be put here... -->


Second, create a file UrlRewriter.xml in the root of your web project, and add rewrite conditions in there:


<?xml version="1.0" encoding="utf-8" ?>


The above code has 3 possible rewrite conditions. If a URL is in the form of "xxxx.php", it is rewritten to "xxxx.aspx". If a URL is in the form "/x/xxxx.php", it is rewritten to "Default.aspx". The third one is a bit more complicated, as it rewrites "search/region/xxxxx/number/yyyyy" to "Default.aspx?region=xxxxx&number=yyyyy". Easy, no?

This is an imported post. It was imported from my old blog using an automated tool and may contain formatting errors and/or broken images.

Leave a Comment


One response

  1. Avatar for Sandesh Daddi
    Sandesh Daddi February 28th, 2008

    Actually I wan to redirect a user from any page to default.aspx.
    It means If user wants to view then
    he/she will be redirected to and output of page which he/she was requesting is shown to him on that default.aspx page.

    Whether it is possible????