IIS URL rewrite with special characters - iis

I am trying to write a rewrite rule in IIS 8.5 that throws 404 error when a string is found in the URL.
My current rule
<rule name="BlockscheduledJobs-Rule2" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*scheduledJobs*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="CustomResponse" statusCode="404" statusReason="Error" statusDescription="Error" />
</rule>
This rule works fine when the URL format is like
https://www.example.com/scheduledJobs/test.aspx
but doesn't work when it is like
https://www.example.com/index.aspx?task=scheduledJobs.test.run.aspx&value=job&result=true
how can i get my rule working for below URL format

Modifying the rule as mentioned below fixed my issue.
<rule name="BlockscheduledJobs-Rule2" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*scheduledJobs.*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="CustomResponse" statusCode="404" statusReason="Error" statusDescription="Error" />
</rule>

Related

IIS redirect with url exception for stripe

I'm working on updating the stripe checkout from a website, I was doing it successfully on localhost but when I put it on live mode on a windows server it stopped working. The issue is that when I should be redirected to the checkout page from stripe, the url is altered and becomes something that doesn't make sense:
The correct url: www.checkout.stripe.com/pay/cs_...
The url that I get redirected to: www.mysite.com/pay/cs_..
I kept thinking what could be the causa of that and I think it's the URL rewrite rule that I have on the windows server. I would like to add an exception to the rule and allow the stripe checkout to initiate, but I have no idea how to do it.
Below is my web.config file:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://localhost:8008/{R:1}" />
</rule>
<rule name="HTTPS" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I figured it out. This is my final web.config
<configuration>
<system.webServer>
<urlCompression doDynamicCompression="false" />
<rewrite>
<rules>
<clear />
<rule name="ReverseProxyInboundRule">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="http://localhost:8008/{R:1}" />
</rule>
<rule name="stripe redirect in checkout" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="/pay/" />
</conditions>
<action type="Redirect" url="checkout.stripe.com{REQUEST_URI}" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
</system.webServer>
My issue was not really understanding the meaning of the options in the URL Rewrite. I checked the course URL Rewrite for Developers and it was really helpful. I was able to solve the issue quickly.
The file you have shown contains the inbound rewrite rules only. But you have an issue with response from your server. Thus, you should fix the outbound rewrite rule in the right config file.

IIS 7 Redirect to new domain

Good morning,
I am trying to create a Rewrite rule in IIS to redirect a specific page to a new url however when I apply the rule no redirect happens what am I missing? Code:
</rule>
<rule name="Redirect" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^(/)?$" />
<add input="{HTTP_HOST}" pattern="domain1/server/sdk/" />
</conditions>
<action type="Redirect" url="https://domain2/rest/" />
</rule>
Try adding REQUEST_URI to the pattern match
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="domain1/server/sdk/" />
match url may also work better as
<match url="^(.*)$" />
also make sure the rule is enabled, your version has enabled="false" on it
Here is my full rule
<rule name="domain1/server/sdk/" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="domain1/server/sdk/" />
</conditions>
<action type="Redirect" url="https://domain2/rest/" redirectType="Permanent" appendQueryString="false" />
</rule>
Excellent thank you very much Mike, I did disable my rule after testing it and it did not work which is why it shows enabled=false however I revised the rule per your advice and that worked for me! Incase this is helpful for anyone else in the future my full rule is:
<rules>
<remove name="Portal Redirect" />
<rule name="(rule name)" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<action type="Redirect" url="Domain2/rest/" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="domain1/server/sdk/rest/" />
</conditions>
</rule>

Priority for outboundRules for URL rewrite module

Will rule #2 get hit if rule #1 does first? Or will it stop at rule #1 for IIS Url Rewrite Module. I am trying to skip webfonts with the specific origin.
<outboundRules>
<rule name="Set Access-Control-Allow-Origin header">
<match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_ORIGIN}" pattern="(http(s)?://((.+\.)?(thestatbook\.com|localhost:3000)))" />
</conditions>
<action type="Rewrite" value="{C:0}" />
</rule>
<rule name="Enable CORS for Fonts">
<match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
<conditions>
<add input="{REQUEST_URI}" pattern="^[^\?]+\.(ttf|otf|eot|woff|woff2|svg)(\?.*)?$" />
</conditions>
<action type="Rewrite" value="*" />
</rule>
</outboundRules>
Will rule #2 get hit if rule #1 does first? Or will it stop at rule #1 for IIS Url Rewrite Module.
As far as I know, both outboundRules wil hit. It will firstly run "Set Access-Control-Allow-Origin header", then "Enable CORS for Fonts". You could write a simple rule to and use postman to test it.
Rule like below:
This rule will modify the Access-Control-Allow-Origin and Server variable.
<outboundRules>
<rule name="removingserverheader" enabled="true" stopProcessing="true">
<match serverVariable="RESPONSE_SERVER" pattern=".*" />
<action type="Rewrite" value="0" />
</rule>
<rule name="Enable CORS for Fonts">
<match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
<conditions>
<add input="{HTTP_ORIGIN}" pattern=".*" />
</conditions>
<action type="Rewrite" value="2" />
</rule>
</outboundRules>
Result:
If you just want only one rule is fired not hit another rule. I suggest you could try to use StopProcessing flag.
It means when the rule action is performed (i.e. the rule matched) and this flag is turned on, it means that no more subsequent rules will be processed and the request will be passed to the IIS request pipeline. By default, this flag is turned off.
Rule like below:
<rewrite>
<outboundRules>
<rule name="removingserverheader" enabled="true" stopProcessing="true">
<match serverVariable="RESPONSE_SERVER" pattern=".*" />
<action type="Rewrite" value="0" />
</rule>
<rule name="Enable CORS for Fonts" enabled="true">
<match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
<conditions>
<add input="{HTTP_ORIGIN}" pattern=".*" />
</conditions>
<action type="Rewrite" value="2" />
</rule>
</outboundRules>
</rewrite>
Result:

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.

IIS URL Rewrite not redirecting subfolders

URL Rewrite rule for redirecting from http to https:
<rewrite>
<rules>
<rule name="http to https" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
However, when you go directly to a subfolder like http://www.example.com/employees via an old bookmark or by typing the address into the browser, it doesn't redirect to https://www.example.com/employees but stays on http://www.example.com/employees.
Oddly enough, we have the same rewrite rules for a dev site, and it will redirect from http://dev.example.com/employees to https://dev.example.com/employees.
Very strange. Any ideas?
This is what I use - I add it to the applicationhost.config file, so it's at the global level. It's a little different then yours, I run many different sites and never have the issue you're describing doing it this way.
<rule name="Root Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{SERVER_PORT}" pattern="^80$" />
<add input="{HTTP_HOST}" pattern="example\.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" />
</rule>

Resources