Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Windows Azure, PHP, ...

About the author

Maarten Balliauw is currently employed as .NET Technical Consultant at RealDolmen. His interests are mainly web applications developed in ASP.NET (C#) or PHP and the Windows Azure cloud platform.
More about me More about me
Send mail E-mail me


ASP.NET MVC Quickly Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn
View Maarten Balliauw's MVP profile

Search

Latest Twitter

    Follow me on Twitter...

    Archive

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright Maarten Balliauw 2012


    ASP.NET URL rewrites using .htaccess-like syntax

    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:


    <httpModules>

        <add name="UrlRewriter" type="MaartenBalliauw.UrlRewriter.Rewriter"/>

        <!-- Other modules can be put here... -->

    </httpModules>

    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" ?>

    <UrlRewriter>

        <Mapping>

            <From><![CDATA[^\/([_a-zA-Z0-9-]+).php]]></From>

            <To><![CDATA[$1.aspx]]></To>

        </Mapping>

        <Mapping>

            <From><![CDATA[^\/([_a-zA-Z0-9-]+)\/([_a-zA-Z0-9-]+)\.php]]></From>

            <To><![CDATA[Default.aspx]]></To>

        </Mapping>

        <Mapping>

            <From><![CDATA[^\/search\/region\/([_a-zA-Z0-9-]+)\/number\/(\d+)]]></From>

            <To><![CDATA[Default.aspx?region=$1&number=$2]]></To>

        </Mapping>

    </UrlRewriter>

    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?


    Categories: ASP.NET | C# | General | Projects

    Comments (1) -

    Sandesh Daddi India |

    Thursday, February 28, 2008 11:52 AM

    Sandesh Daddi

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

    Whether it is possible????



    Comments are closed