IIS URL Rewrite Not Working As Expected - iis

I had this URL Rewrite like this:
<rewrite>
<rules>
<rule name="Item Detail">
<match url="^item/(?!(action/edit)|(action/preview))([_0-9a-z-()#$!']+)"/>
<action type="Rewrite" url="item?item={R:1}" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
In hope to rewrite item/something-dash-something as item?item=something-dash-something. The rewrite rule does working as expected in doing the initial intention. Unfortunately the (?!(action/edit)|(action/preview)) part did not exclude preview mode (action/preview) as expected despite Page Designer mode (action/edit) works properly. I am not sure what am i missing since testing the regex in Regex 101 shows that it works as expected. I also have tested it in RegexStrom also.
EDIT:
Tested Victor's suggestion, to no avail. The Rewrite still fires for action/preview. The next silly item is (inspired by Victor answer), tested this rule and it does not work as expected also:
<rewrite>
<rules>
<rule name="Item Detail">
<match url="^item/([_0-9a-z-()#$!']+)"/>
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_URI}" pattern="(action/edit)" negate="true"/>
<add input="{REQUEST_URI}" pattern="(action/preview)" negate="true"/>
</conditions>
<action type="Rewrite" url="item?item={R:1}" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
Anyone had better idea..? Did i miss something?
EDIT 2: This one still not working also..
<rewrite>
<rules>
<rule name="Item Detail" stopProcessing="true">
<match url="^item/(?!action/)([_0-9a-z-()#$!']+)"/>
<action type="Rewrite" url="item?item={R:1}" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
i have no idea why preview seems to be ignored and passed the url rewrite.
EDIT 3:
Stupid me, one of the widget still assumes there will always query string. Case closed, the stopProcessing="true" is a good addition nonetheless..

You need to exclude preview and edit URLs with condition:
<rule name="Item Detail">
<match url="^item/([_0-9a-z-()#$!']+)" />
<conditions>
<add input="{REQUEST_URI}" pattern="(action/edit)|(action/preview)" negate="true" />
</conditions>
<action type="Rewrite" url="item?item={R:1}" appendQueryString="false" />
</rule>

Related

IIS C# Rewriter

I have just started looking at URL the Rewriter module in IIS.
The code below is what I have that works (It removes www from all the URLs to the site)
<rewrite>
<rules>
<rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{CACHE_URL}" pattern="*://www.*" />
</conditions>
<action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
However, we have a third party API that posts to http://www.example.com/api/controller/method that no longer works. (It is being served 405's)
The problem is (I believe) that whilst WebAPI allows www.example.com/api/controller/method to POST, it is seeing example.com/api/controller/method as a different URL and is not allowing POST.
However the followingconfig section does not work. (Note, removing the wildcard section means it does work, so I am as convinced as I can be that it is this that is causing the 3rd Party to fail). Though testing the PassAPICall in IIS in the configure rewriting section passes. Is it that I have misunderstood the intent of stopProcessing, and in fact all it does is stop processing for that rule, so it then falls throug hto the following condition?
<rules>
<clear />
<rule name="PassAPICall" stopProcessing="true">
<match url="/api/Blah/QuotationComplete" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="None" />
</rule>
<rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{CACHE_URL}" pattern="*://www.*" />
</conditions>
<action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
</rule>
</rules>
Was a simple syntax issue, I needed to be a little more explicit with the Rule than the IIS test tool led me to believe.
changing
<match url="/api/Blah/QuotationComplete" />
to
<match url="^api/Blah/QuotationComplete$" />
did it.

URL Redirect web.config

I have the following URL
https://www.abcsite.com/?data=w35VTqIaVXWfi4GmESj8EGCG1cdF2aT%2BUF3D?utm_source=external%20system&utm_campaign=external%20system&utm_medium=EX.com&utm_content=MMB
which has extra ? at the end. which I Would like to remove and replace with "&"
I am using following rule but it is not working. can you review this and tell me what I am doing wrong.
<rewrite>
<rules>
<rule name="fixdata" stopProcessing="true">
<match url="\?(.*)\?(.*)$" ignoreCase="false" />
<action type="Redirect" redirectType="Permanent" url="http://www.example.com/{R:1}&{R:2}" />
</rule>
</rules>
</rewrite>
Thanks.
You rule should be like this:
<rule name="fixdata" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="(.*)\?utm_source(.*)$" />
</conditions>
<action type="Redirect" url="{R:0}?{C:1}&utm_source{C:2}" appendQueryString="False" />
</rule>
Your rule didnt work, because <match url= contains path without query string. You need to create additional condition to match query string with your regexp pattern

Rewrite url query string to look like directory

Well, I am not good in English actually so I couldn't explain my problem shortly in the title...
Shortly, I need to make
http://<SITE>/user/?id=<x>
to
http://<SITE>/id<x>
Is it possible in Microsoft Azure using web.config?
Inside your web.config you should insert inside <system.webServer> section this rules:
<rewrite>
<rules>
<rule name="Redirect for user" stopProcessing="true">
<match url="^user" />
<conditions>
<add input="{QUERY_STRING}" pattern="id=(\d+)" />
</conditions>
<action type="Redirect" url="/id/{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
It will redirect urls like that http://<SITE>/user/?id=<x> into http://<SITE>/id<x> where <x> is digits

Azure Web App URL Rewrite not working

I'm trying to get a basic URL rewrite to work for my Azure web App
<system.webServer>
<rewrite>
<rules>
<rule name="SalesCloudGmail" stopProcessing="true">
<match url="/SalesCloudGmail.aspx" />
<action type="Redirect" url="/SalesCloudGmail" />
</rule>
</rules>
</rewrite>
</system.webServer>
Was expecting to open the URL .../SalesCloudGmail.aspx and see .../SalesCloudGmail in the Address bar???
What Am I missing
Give this a try, your regex pattern matching is a little off based on how you're using it. But this should work or at least get you much closer.
<rule name="SalesCloudGmail" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="^\/SalesCloudGmail\.aspx.*" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/SalesCloudGmail" />
</rule>
Please try below rewrite rule:
<rewrite>
<rules>
<rule name="SalesCloudGmail" stopProcessing="true">
<match url="^\/SalesCloudGmail\.aspx$" />
<action type="Redirect" url="/SalesCloudGmail" />
</rule>
</rules>
</rewrite>
Please following this article to test your match pattern.

How to append query string & value via IIS rewrite rule?

I'm running IIS 7 with the offical rewrite rule module installed. I'd like to create a rewrite rule to match this URL:
http://www.sample.com/en-us/test.aspx?q=keyword
After rewriting the expected result would be:
http://www.sample.com/en-us/test.aspx?q=keyword&flag=value
How can I create a rule to implement this?
I've tested the following rule, but no luck, it always got redirect loop error:
<rewrite>
<rules>
<rule name="test" stopProcessing="true">
<match url="(.*)/test\.aspx(.(?!flag=value))*$" />
<action type="Redirect" url="{R:0}&flag=value" appendQueryString="false" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
Found the solution by myself, just share it.
<rewrite>
<rules>
<rule name="Redirect for download result page" stopProcessing="true">
<match url="(.*)/test.aspx(.*)" />
<action type="Redirect" url="{R:1}/test.aspx?rf=sp" appendQueryString="true" redirectType="Found" />
<conditions>
<add input="{QUERY_STRING}" pattern="flag=value" negate="true" />
</conditions>
</rule>
</rules>
</rewrite>

Resources