Reputation: 63
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}&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
Upvotes: 1
Views: 475
Reputation: 63
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}&format={C:2}" appendQueryString="false" />
</rule>
Upvotes: 1
Reputation: 16440
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.
Upvotes: 0
Reputation: 11762
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}&format={C:1}" />
</rule>
Upvotes: 1