1

I am trying to make a rule for URL Rewrite 2.0 my internal URL is

http://someserver.net/in40/data/getcase.ashx?type=Good&format=json

("in40" is an application)

I need the following URL to work

http://someserver.net/in40/data/getcase/Good?fmt=json

rule

<match url="^data/getcase/([_0-9a-z-]+)\?fmt=([_0-9a-z-]+)$" />
<action type="Rewrite" url="data/getcase.ashx?type={R:1}&amp;format={R:2}" />

doesn't work


but if I change \?fmt= to /

<match url="^data/getcase/([_0-9a-z-]+)/([_0-9a-z-]+)$" />

then the following URL works fine

http://someserver.net/in40/data/getcase/Good/json 

How to make the rewrite rule for

http://someserver.net/in40/data/getcase/Good?fmt=json
3
  • I wouldn't have thought you needed the ^ giving the string you are matching is not at the start of the string... Commented Oct 4, 2013 at 14:18
  • it is start of the string. The base part of the URL is "someserver.net/in40". Commented Oct 4, 2013 at 14:25
  • And, as I said, the ^data/getcase/([_0-9a-z-]+)/([_0-9a-z-]+)$ works fine. (but I tried to remove the ^ symbol :) Commented Oct 4, 2013 at 14:27

3 Answers 3

1

The problem is that [_0-9a-z-]+ in your regex doesn't allow Good as it starts with an upper G. One solution would be to make sure the ignoreCase option is turned on for the path.
The second issue is that the query string should be treated in the condition and not in the path. In your case, it would go as follow:

<rule name="My rewrite" stopProcessing="true">
    <match url="^data/getcase/([_0-9a-z-]+)$" ignoreCase="true" />
    <conditions>
        <add input="{QUERY_STRING}" pattern="^fmt=([_0-9a-z-]+)$" />
    </conditions>
    <action type="Rewrite" url="data/getcase.ashx?type={R:1}&amp;format={C:1}" />
</rule>
Sign up to request clarification or add additional context in comments.

2 Comments

thank you, the "ignoreCase" param should be there, but it still doesn't work. It looks like UrlRewrite 2.0 ignores the \? part. Because when I change it to \. then everything goes fine....... someserver.net/in40/data/getcase/Good.fmt=json
Thanks, cheesemacfly :) your rule definition works fine. I have added my solution, but I am going to choose your answer. Thanks for your help :)
1

My previous rule definitions was wrong and URL Rewrite mudule couldn't see the "?" symbol because I didn't have a "conditions" section and I had to include it. And even after that I had wrong "input" parameter in rule definition - {QUERY_STRING}. When the module "sees" the

<add input="{QUERY_STRING}..... 

string then it will search for {QUERY_STRING} which is a part of URL right AFTER the "?" symbol :) I have replaced {QUERY_STRING} with {REQUEST_URI} to match entire URL instead The following example works for me.

<rule name="Boo" stopProcessing="true">
  <match url="^(.+)" />
  <conditions>
    <add input="{REQUEST_URI}" pattern="data/getcase/([_0-9A-Za-z-]+)\?fmt=([_0-9A-Za-z-]+)$" ignoreCase="true" />
  </conditions>
  <action type="Rewrite" url="data/getcase.ashx?type={C:1}&amp;format={C:2}" appendQueryString="false" />
</rule>

Comments

0

The regex engine needs a backslash in front of the question mark to interpret it as a literal rather than the optional quantifier, which is why you'll have to escape the backslash itself: \\? This process is called double-escaping.

When writing regexes in C#, you usually use the @ sign in front of the pattern string so you don't have to double-escape backslashes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.