Previous Next

Thread: Migrating from UrlRewritingNet.UrlRewriter.dll to IIS Url Rewrite Module

Last post 08-05-2008 6:26 PM by MCUSA. 5 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (6 items)

Sort Posts:

  • 07-17-2008, 2:31 PM

    • MCUSA
    • Top 500 Contributor
    • Joined on 11-25-2005, 7:37 PM
    • Oregon
    • Posts 10

    Migrating from UrlRewritingNet.UrlRewriter.dll to IIS Url Rewrite Module

    Hi we have been using UrlRewritingNet and because of the IIS integration in IIS 7 were going to migrate to the IIS URL rewrite module.

    However I have noticed a few different things that could just be configiration differences but I don't readily see how to fix these.

    In the past we could use relative urls in our master page for things like style sheets as long as the HEAD had a runat server and it would generate the proper link to work in the rewritten url such as ../../../../Style/stylesheet.css. This doesn't seem to work in the IIS module?

     We also used IMG tags with a runat server and the ~ tilde to generate the proper URL. This doesn't seem to work either?

     Do I need to create specific rules to handle this? Can anyone point me in the right direction?

  • 07-17-2008, 3:25 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 3:38 PM
    • Redmond, WA
    • Posts 204

    Re: Migrating from UrlRewritingNet.UrlRewriter.dll to IIS Url Rewrite Module

    The URL rewrite module is not aware of the ASP.NET root operator: "~". So, first thing that should be done is to change the rule pattern and substitution URL to not contain "~".

    For example this rule:

    <add name="Rule1" virtualUrl="^~/(.*)/Detail(.*).aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/Default.aspx?language=$1&amp;id=$2" ignoreCase="true" />

    would look something like this:

    <rule name="Rule1">
       <match url="^(.*)/Detail(.*).aspx$">
       <action type="Rewrite" url="Default.aspx?language={R:1}&amp;id={R:2}">
    </rule>

    http://ruslany.net
  • 07-17-2008, 3:50 PM In reply to

    • DanielVL
    • Top 50 Contributor
    • Joined on 10-07-2006, 6:00 PM
    • Redmond, WA, USA
    • Posts 74

    Re: Migrating from UrlRewritingNet.UrlRewriter.dll to IIS Url Rewrite Module

    Additionally, be aware that the regex expression would also match:

    Product/DetailABaspx

    And will capture just 'A'.

    You need to change "(.*)/Detail(.*).aspx" to "(.*)/Detail(.*)\.aspx"; remember that the expression "." means any character, you need to escape it in order to match the character '.'

    For this kind of simple expressions, you could switch to Wildcards, it has better performance.

    <rule name="Rule1" patternSyntax="Wildcard">
      <match url="*/Detail*.aspx" />
      <action type="Rewrite" url="Default.aspx?language={R:1}&amp;amp;id={R:2}" />
    </rule>

    You can find additional information here: http://blogs.msdn.com/danielvl/archive/2008/06/06/url-rewrite-for-iis-7-0-regular-expressions-and-wildcards.aspx

    Daniel Vasquez Lopez
    IIS Team
  • 07-17-2008, 3:57 PM In reply to

    • MCUSA
    • Top 500 Contributor
    • Joined on 11-25-2005, 7:37 PM
    • Oregon
    • Posts 10

    Re: Migrating from UrlRewritingNet.UrlRewriter.dll to IIS Url Rewrite Module

    I actually have the rewriting working as far as getting to the correct urls. My main question above about the ~ tilde. Is from IIS 6 and running this from UrlRewritingNet that if I had the following code in my default master

    <img id="imgHeader" src="~/Images/header.jpg" runat="server" alt="My Header" style="border: 0px;" />

    and the URL was rewritting from say www.mydomain.com/1/apage.aspx to something like www.mydomain.com/rewritepage.aspx?value=1

    Then in the old working model the image url would be rendered as ../../images/header.jpg instead - however now it is getting rendered with the url rewritten params such as www.mydomain.com/1/images/header.jpg which doesn't exist.

     

    Same as the Style sheet which is in the root directory under a folder called styles which 'should' be rendered as ../../styles/stylesheet.css

     

    Does this make more sense? Is this more an IIS 7 problem than the rewrite mod?

  • 07-17-2008, 5:24 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 3:38 PM
    • Redmond, WA
    • Posts 204

    Re: Migrating from UrlRewritingNet.UrlRewriter.dll to IIS Url Rewrite Module

    Yes, that makes sense.

    The reason why this works with URLRewriting.NET is because it has a logic to fix up the relative path for the ASP.NET application. URL rewrite module does not do that, hence the "~/" - style links become broken after rewriting.

    One way to fix that is to add the following event handler to your asp.net pages:

        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Request.ServerVariables["HTTP_X_ORIGINAL_URL"] != null)
                Context.RewritePath(VirtualPathUtility.GetDirectory(Request.ServerVariables["HTTP_X_ORIGINAL_URL"]) + VirtualPathUtility.GetFileName(Request.ServerVariables["HTTP_X_ORIGINAL_URL"]));
        }

    http://ruslany.net
  • 08-05-2008, 6:26 PM In reply to

    • MCUSA
    • Top 500 Contributor
    • Joined on 11-25-2005, 7:37 PM
    • Oregon
    • Posts 10

    Re: Migrating from UrlRewritingNet.UrlRewriter.dll to IIS Url Rewrite Module

    Just a quick note but when using the method HTTP_X_ORIGINAL_URL with a App_Browser based method like Scott Gu uses in his example will break non rewritten page post backs unless you use

     httpContext.Current.Request.RawUrl

     

    http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

Page 1 of 1 (6 items)
Page view counter