+ Reply to Thread
Results 1 to 10 of 10

Thread: IIS Rewrite Regex

  1. #1
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    IIS Rewrite Regex

    I have installed IIS rewrite module for IIS7 and although it's working I'm having a slight problem with the Regex.

    It's just a very simple URL rewrite where I want to have
    Code:
    /page/page2/id/ to go to
    pagepage2.aspx?ID=id
    
    This works fine as it is, but I've found browsers (IE specifically) knock the last / off the URL in the autocomplete dropdown and my regex won't handle it.

    I currently have 3 rewrite rules, in order of
    1
    Code:
    ([_a-z0-9]+)/([_a-z0-9]+)/([_a-z0-9]+)[/]?
    goes to
    {R:1}{R:2}.aspx?ID={R:3}
    
    2
    Code:
    ([_a-z0-9]+)/([_a-z0-9]+)[/]?
    goes to
    {R:1}{R:2}.aspx
    
    3
    Code:
    ([_a-z0-9]+)[/]?
    goes to
    {R:1}.aspx
    
    But if I try loading page/1 it loads the first time, but when it does a partial postback on the page the rewrite ends up as pagepage1.aspx

    I don't understand why it's doubling up the first variable. If I try page/1/ it works fine. It's just the last / I can't get to work right.

    Hope that makes sense.

    Any ideas?

    <edit>Actually, it works on the first partial postback and then adds in the extra section to the rewrite on the second partial postback</edit>

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    OK. I found the problem.

    .NET was automatically adding the action variable to the form, but because of the rewrite it was adding some ../ to the url.

    I added this in my page_load and it solved the problem
    Code:
    Page.Form.Action = HttpContext.Current.Request.Url.ToString
     


  3. #3
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    OK. That doesn't seem to have solved the problem.

    Basically I want a regex that will catch:-
    Code:
    page/1/ or page/1
    and redirect to
    page1.aspx
    
    Currently what I have is
    Code:
    ([_a-z0-9]+)/([_a-z0-9]+)/
     

    I have tried changing the last / to ([/])? and /? and (/)? but none of them seem to work.

    How can I make the last / optional?

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Hey R, give this a try:
    Code:
    ^([_a-z0-9]+)/([_a-z0-9]+)/?$
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  5. #5
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by jmurrayhead View Post
    Hey R, give this a try:
    Code:
    ^([_a-z0-9]+)/([_a-z0-9]+)/?$
    
    Hey J...No dice I'm afraid...I think I've already tried that..

    It doesn't seem to like anything at the end of the URL regex....I tried the add trailing slash from http://ruslany.net/2009/04/10-url-re...ps-and-tricks/ and it didn't seem to like that either...

    Do any sites cope with this issue or do they all just only deal with urls ending with /?

  6. #6
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Post everything you have for the URL Rewriting.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  7. #7
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Here you go J.....

    I suspect there's a better way of doing it, but this is what I have
    Code:
        <rewrite>
          <rules>
            <rule name="RewriteUrl1" stopProcessing="true">
              <match url="([_a-z0-9]+)/([_a-z0-9]+)/([_a-z0-9]+)/" />
              <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
              </conditions>
              <action type="Rewrite" url="{R:1}{R:2}.aspx?ID={R:3}" />
            </rule>
            <rule name="RewriteUrl2" stopProcessing="true">
              <match url="([_a-z0-9]+)/([_a-z0-9]+)/" />
              <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
              </conditions>
              <action type="Rewrite" url="{R:1}{R:2}.aspx" />
            </rule>
            <rule name="RewriteUrl3" stopProcessing="true">
              <match url="([_a-z0-9]+)/" />
              <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
              </conditions>
              <action type="Rewrite" url="{R:1}.aspx" />
            </rule>
          </rules>
        </rewrite>
    
    This works when the URL ends in / but not if it doesn't

  8. #8
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    R,

    Did you try placing '^' and '$' in all your expressions, like I showed in my example? Also, I think the order the rules are in matters.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  9. #9
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by jmurrayhead View Post
    R,

    Did you try placing '^' and '$' in all your expressions, like I showed in my example? Also, I think the order the rules are in matters.
    Hey J....I could have sworn I did, but I just tried it again and it seems to have sorted it...Awesome!!

    Thanks so much J...Was pulling my hair out...

    I have the order so that if I have customers/view/123/ that doesn't get caught by the rewrite 3...Do you think the order ought to change?

  10. #10
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Quote Originally Posted by richyrich View Post
    Hey J....I could have sworn I did, but I just tried it again and it seems to have sorted it...Awesome!!

    Thanks so much J...Was pulling my hair out...
    I rock

    Quote Originally Posted by richyrich View Post
    I have the order so that if I have customers/view/123/ that doesn't get caught by the rewrite 3...Do you think the order ought to change?
    I think this is one of those things that is best answered by trial and error...I'm not too sure
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


+ Reply to Thread

Similar Threads

  1. Javascript Regex Pattern for decimal number(18,5)
    By guddu in forum JavaScript Programming
    Replies: 12
    Last Post: January 27th, 2009, 11:28 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO