Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 13, 2012 @ 19:17
    Bo Damgaard Mortensen
    0

    Querystring URL rewrite rule

    Hi all,

    I'm trying to use the Url rewriter to rewrite a querystring to a more clean-looking URL. The snippet I'm using is this:

    <add name="Gallery"
    virtualUrl="^~/mypage/(.*)"
    rewriteUrlParameter="ExcludeFromClientQueryString"
    destinationUrl="~/mypage.aspx?myquerystring=$1"
    ignoreCase="true" />

    This works a little too well since mypage.aspx contains childnodes. If a user clicks on a link to a childnode of mypage, they get redirected to mypage and not to the childnode itself.

    Any way to fix this? :-)

    The current querystring url looks like this: mypage.aspx?myquerystring=1234 and I want it to look like this: mypage/1234.

    Any help/hint is greatly appreciated!

    Thanks in advance.

  • Stephen 767 posts 2273 karma points c-trib
    Nov 13, 2012 @ 19:38
    Stephen
    0

    Do you also want to redirect mypage/child/1234 to mypage/child?myquerystring=1234?

    How can we differentiate betweens mypage/1234 and mypage/child? Because 1234 is numbers?

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 13, 2012 @ 19:42
    Bo Damgaard Mortensen
    0

    Hi Stephen,

    Nope, the mypage/child/1234 to mypage/child?myquerystring=1234 is not needed :-) Only the parent page: mypage.aspx.

    I guess there's not really any way to differentiate the two URLs :-/ The querystring value isn't only numbers, it's actually theme-names such as "christmas", "halloween" etc..

    So, is this do-able?

    Thanks again!

    - Bo

  • Stephen 767 posts 2273 karma points c-trib
    Nov 13, 2012 @ 19:50
    Stephen
    0

    Er... if there's no way to differentiate, then I don't see how this is do-able. Unless there's a finite list of themes and you write one rule per theme.

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 13, 2012 @ 19:53
    Bo Damgaard Mortensen
    0

    Hi Stephen,

    Ouch! That's what I was nervous about. So there's absolutely no way to 'nicefy' a querystring when the node has got any childnodes?

  • Stephen 767 posts 2273 karma points c-trib
    Nov 13, 2012 @ 19:59
    Stephen
    0

    Well, how can we tell that mypage/xmas should be rewritten but mypage/candies should not?

    An easy solution would be to prefix your themes... eg mypage/theme/xmas or mypage/xx-xmas...

    Otherwise I don't see how you can do it with rewriting.

    That being said,

    Write a 404 handler, check if you're looking for .../mypage/something, and redirect to .../mypage?q=something there. That way, the children will be handled first, and if no child is found then you fallback on themes...

    Or, create an actual child per theme with an internal redirect to mypage.

    But... thinking about how routing should evolve... I'd go with a 404 handler.

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Nov 13, 2012 @ 21:31
    Morten Bock
    0

    I would probably do something like this with the urls:

    /mypage/t/christmas

    Then you can match it in your rewrite with

    ^~/mypage/t/(.*)

    And that would only prevent you from seeing subpages if there is a page named "t".

    Still makes the url nicely readable, and lets you have subpages at the same time.

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 13, 2012 @ 21:51
    Bo Damgaard Mortensen
    0

    Hey Stephen and Morten,

    Writing a 404 handler would be a nifty way of doing it, but time is not with me on this one :-( Morten, your way works perfect! That's what I'm going for :-) Only thing that's left now is to tell the site that a querystring like this: "christmas & fun" is a legal querystring. It gives me this exception:

    A potentially dangerous Request.Path value was detected from the client (&).

    Is there any way to tell the site that these special chars are o.k? :-)

    Thanks again!

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Nov 13, 2012 @ 22:58
    Chriztian Steinmeier
    0

    Hi Bo,

    Technically, I think you need to escape a string like that, e.g. to appear in a QueryString it'd have to be "christmas%20%26%20fun" ...

    /Chriztian

  • Stephen 767 posts 2273 karma points c-trib
    Nov 14, 2012 @ 08:58
    Stephen
    0

    Anything you put in the request.path needs to be url-encoded. Else you're going to break things. Think of a theme such as "xmas? fun?" => IIS will see the "?" as introducing a querystring.

    See http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.urlencode.aspx

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 14, 2012 @ 12:33
    Bo Damgaard Mortensen
    0

    Thanks Chriztian and Stephen :-)

    I talked them into using "og" (danish word for "and") instead of the & character. No problem there! So everything works just as it should now.

Please Sign in or register to post replies

Write your reply to:

Draft