+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / URL Rewrite with multiple query string parameters

URL Rewrite with multiple query string parameters

4 posts, 0 answered
  1. Adam
    Adam avatar
    63 posts
    Registered:
    30 Jan 2008
    28 Jan 2009
    Link to this post
    I'm trying to use multiple querystring parameters in a URL Rewrite rule like the following:

    URL = /123/product_2.aspx  rewritten to: product.aspx?id=2&cat=123

    Where 123 is the category id and 2 is the product id.

    The rewrite rule does not like the ampersand (&).  How can I get the rewrite rule to allow the ampersand? 

    <urlrewrites>
       <
    rule>
          <
    url>/(.*)/product_(.*)\.aspx</url>
          <
    rewrite>/product.aspx?CatID=$1&id=$2</rewrite>
        </
    rule>
    </
    urlrewrites>

     

     

     

     

     

     

  2. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    29 Jan 2009
    Link to this post
    Does it give an error?  If so, what is the error?

    Or does the rewriting rule simply not work as expected?  What does it do instead?

    One random thought I have is that (.*) is a very greedy pattern.  You might try (.*?) instead.  I'm not sure this has anything to do with your issue though.

    Gabe Sumner
    http://www.sitefinitywatch.com/
  3. Adam
    Adam avatar
    63 posts
    Registered:
    30 Jan 2008
    29 Jan 2009
    Link to this post
    It gives compile error that the & is an illegal character.  Changing it to &amp; helps but thy rewrite still doesn't work.. I think there is a problem with my rewrite logic as you had suggested.

    What gets placed into the $1 and $2 variables?  Is $2 even possible because none of the examples that I could find show more than one of those $ vars?  I was just shooting in the dark for that one. 

    Also What does this pattern do?  (.*?)

    Thanks,
    Adam
  4. Adam
    Adam avatar
    63 posts
    Registered:
    30 Jan 2008
    29 Jan 2009
    Link to this post
    I found the answer to my question by looking up how Regular Expressions work. 

    it seems that the $1, $x variables are assigned in order from left to right to anything in the parenthesis ()

    for example...

    URL = domain.com/category-name-123/product-name-123.aspx
    pattern = domain.com/([A-Za-z\-]*)([\d]+)/([A-Za-z\-]*)([\d]+)\.aspx

    which would give the following variables and values:
    $1 = category-name-
    $2 = 123
    $3 = product-name-
    $4 = 123

    This is exactly what I was needing.
Register for webinar
4 posts, 0 answered