Can I add a condition to exclude an IIS URL Rewrite rule if the request type is POST? I was not able to find it anywhere in the documentation.
I don't want to lower-case my URL if the HTTP method is POST.
<rule name="LowerCaseRule" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
</rule>
The solution is filtering the input {REQUEST_METHOD}, negating every request matching POST, like:
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" ignoreCase="true" negate="true" />
</conditions>
Related
I have multiple domains bound to one site. For one domain, I would like to leave everything untouched by the Rewrite module. For the other domain, for example: www.example.com, I wish to allow every request down this path www.example.com/allowedpath yet for any other requests from www.example.com and below, return a 404.
From your description, I understand that you want to achieve requirement below.
Domain1.com -> Should not be affected by Rewrite rule
Example.com -> Example.com/allowed_path
Example.com/any_other_path -> Abort request
To achieve the above requirement, I have created 2 rewrite rules below.
<rules>
<clear />
<rule name="Rule-1" stopProcessing="true">
<match url="^$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="/test_path" />
</rule>
<rule name="Rule-2" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="example.com" />
<add input="{REQUEST_URI}" pattern="/test_path" negate="true" />
</conditions>
<action type="AbortRequest" />
</rule>
</rules>
Test result with Domain1.com
Test result with Example.com
Further, you could modify these rules as per your own requirement.
I want to write redirect rules in IIS 10. I googled it and spent half of my day figuring out but no luck so I am posting it to get some solution.
https://testing.app.com/apptest should redirect to https://testing.app.com/apptest/account/login
https://testing.app.com/apptest/ should redirect to https://testing.app.com/apptest/account/login
https://test-apptest.testing.app.com/ should redirect to https://test-apptest.testing.app.com/account/login
https://test-apptest.testing.app.com should redirect to https://test-apptest.testing.app.com/account/login
But when user types url https://testing.app.com/apptest/account/login or https://test-apptest.testing.app.com/account/login then it should not redirect anywhere and it should stay as it is.
<rewrite>
<rules>
<rule name="Test1" stopProcessing="true">
<match url="account/login" />
<action type="None" />
</rule>
<rule name="Test2">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="testing.app.com" />
<add input="{REQUEST_URI}" pattern="^/apptest" />
</conditions>
<action type="Redirect" url="https://testing.app.com/apptest/account/login" appendQueryString="false" />
</rule>
</rules>
</rewrite>
We just add an anchor point to the regular expression so that precisely matches the segment ‘/apptest’.
Updated
<system.webServer>
<rewrite>
<rules>
<rule name="MyRule1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="^((/apptest)?)/?$" />
</conditions>
<action type="Redirect" url="https://{http_host}{C:1}/account/login" />
</rule>
</rules>
</rewrite>
</system.webServer>
Explanation
Since the hostname changed and will subsequently be appended in the redirection URL, I replace it with {http_host} server variable to follow it. Besides, {Request_URI} will return the URL path and {C:1} will return either "/apptest" or "". therefore I append it into the redirection URL.
The meaning of every server variable.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#accessing-url-parts-from-a-rewrite-rule
Finally, please don’t forget to install the URL Rewrite extension before applying the rules.
https://www.iis.net/downloads/microsoft/url-rewrite
Here is a quick reference of the regular expression from Microsoft documentation.
https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
Feel free to let me know if the problem persists.
I'm using Asp.Net web forms.In my URL I don't want to expose after the character '?'. I want the regular expression pattern to achieve this using IIS 10. I have tried this "Security(.+)$" but it doesn't work.
From this: www.some.com/Security/login.aspx?name=dfdf
To this: www.some.com/Security/login.aspx
If you don't want to expose query string.
1.you should redirect any request to www.some.com/Security/login.aspx?name=dfdf to www.some.com/Security/login.aspx.
2.Then you have to rewrite request from www.some.com/Security/login.aspx back to www.some.com/Security/login.aspx?name=dfdf
Just keep in mind that, this rule can only rewrite back to static query string ?name=dfdf.
If you need to need to dynamic rewrite the URL, then you may need to add a custom request header to save the query string. So that backend server will know where should it rewrite back.
<rules>
<rule name="redirect rule" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/Security/login.aspx\?(.+)" />
</conditions>
<action type="Redirect" url="Security/login.aspx" appendQueryString="false" redirectType="Temporary" />
</rule>
<rule name="rewrite back" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/Security/login.aspx$" />
</conditions>
<action type="Rewrite" url="Security/login.asp?name=dfdf" appendQueryString="false" />
</rule>
</rules>
I need to write a redirect to strip off parameters when a certain one exists, but I would like the redirect to be the same as the incoming url, just without the parameters. This is what I had and it doesn't work. I thought if I put the request URI in there without appending the querystring, then it would work but it results in a loop. Has anyone done this before?
<rule name="Remove parameters" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="page=([0-9]+)" />
</conditions>
<action type="Redirect" url="{REQUEST_URI}" appendQueryString="false" />
The problem with jallen's answer is that the whole query string will be removed, which may not be ideal if you want to maintain certain parts of it. An alternative would be as follows:
<rule name="Remove paging parameters" stopProcessing="true">
<match url="(.*)?$" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="(.*)(page=.+)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}{C:3}" appendQueryString="false" />
</rule>
C:1 = Everything before the matching page parameter
C:2 = Is the
match itself, and what we want to exclude
C:3 = Everything after the
matching page parameter
Hence, we use the redirect {C:1}{C:3} to exclude only the page query string.
Got it with the following:
<rule name="Remove paging parameters" stopProcessing="true">
<match url="(.*)?$" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="page=([0-9]+)" />
</conditions>
<action type="Redirect" url="{R:1}" appendQueryString="false" />
For local dev testing, I need to catch all requests to www.somedomain.com/XXX (where X is the path), and send them on to localhost/somevdir/XXX.
I've added this to my HOSTS file (c:\windows\system32\drivers\etc):
127.0.0.1 www.mydomain.com
Then, in IIS8 (Windows 8), I've added a binding to my "Default Web Site" for the host www.mydomain.com. This works, I can now browse www.mydomain.com/test.html and see a test html page. My virtual dir is inside the Default web site. I then add a URL Rewrite URL to the web site for the last bit:
<rewrite>
<rules>
<rule name="mydomain.com" stopProcessing="true">
<match url="^www.mydomain.com/(.*)" />
<action type="Rewrite" url="localhost/MyVDir/{R:1}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
But - that doesn't work. I get a 404 so it looks the the match never happens. I've tried redirect and rewrite, and I've tried without the ^ in the regex and a few other regex tweaks. Can someone explain what I've done wrong?
I think the following should work:
<rule name="mydomain.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(mydomain\.com|www\.mydomain\.com)$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="http://localhost/MyVDir/{R:1}" redirectType="Temporary" />
</rule>
The match on any URL makes sure the conditions are checked, and the HTTP_HOST server variable seems the most reliable way of checking the requested hostname. You could remove the REQUEST_FILENAME input condition, but it works as quite a nice sanity check to make sure static files are always served.
The following is better for catching both www. and non-www. versions of the domain so that you don't have to write the domain twice, which could possibly cause errors with being twice as likely to write a typo. (The parenthesis with the ? character means optional in regex terms.)
<rule name="mydomain.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(www.)?mydomain\.com$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="http://localhost/MyVDir/{R:1}" redirectType="Temporary" />
</rule>