Url redirect through IIS with query string - iis

I am trying to use iis url rewrite module to redirect the post requests with query string to some other url in the same domain. For example:
Original request url:
https://www.example.com/ABC/DEF/GHI/JKL?q=<variableValue>&t=<variableValue>
Redirected URL:
https://www.example.com/DEF/GHI/JKL?q=<variableValue>&t=<variableValue>
Request method is POST and it should be redirected using the same method with same request data and query string. I am not able to find an example. This is what I am trying to follow:
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

you could try this rule
<rule name="query string redirect" stopProcessing="true"><match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.domain1.com" />
<add input="{HTTPS}" pattern="on" />
<add input="{REQUEST_URI}" pattern="/ABC/DEF" />
<add input="{QUERY_STRING}" pattern="q\=([a-zA-Z0-9]+)\&t=([a-zA-Z0-9]+)" />
</conditions>
<action type="Redirect" url="http://www.domain1.com/DEF/GHI/JKL?q={C:1}&t={C:2}" appendQueryString="false" />
</rule>

Related

Regex does not work for {URL} in the URL rewrite in IIS

Using URL rewrite in the IIS on windows 10. I want to check specific url in the IIS.
Below is the condition I need to check.
www.mtl173 or http://www.mtl173 or http://www.mtl173/ or mtl173/
Above all should be valid and should redirect to the given path.
I have checked {URL} so far as following.
<rule name="RuleForSite1" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="^(http\:\/\/)?(www\.)?mtl173(\/)?$" />
</conditions>
<action type="Redirect" url="www.mtl173/app1/" />
</rule>
Note: when I test all the URLs in the IIS, all test passed but when I run the same URL from the browser, it shows me the default IIS page and does not redirect me to the app1/.
However, the above does not work and does not redirect the URL to the given path.
Any remediations for the issue I am facing?
Expected result:
When I enter any of the following URL, it should redirect me to the /app1.
www.mtl173
http://www.mtl173
http://www.mtl173/
mtl173/
You can use below url rewrite rule.
<rule name="RuleForSite1" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.mtl173|mtl173$" />
<add input="{REQUEST_URI}" pattern="app1" negate="true" />
</conditions>
<action type="Redirect" url="http://www.mtl173/app1/" />
</rule>
You can learn about server variable from below links:
IIS Server Variables

IIS Rewrite based on query string value

I am new to IIS Rewrite, Need someone to assist me,
I am in a scenario to redirect to another domain based on querystring value.
Ex:
Request:
http://localhost:50743/api/Values?stdId=1000&stdName="Test"
http://localhost:50752/api/Values?stdId=1001&stdName="Test1"
http://localhost:50753/api/Values?stdId=1002&stdName="Test2"
http://localhost:50754/api/Values?stdId=1004&stdName="Test4"
I just need to validate the query string and redirect the request to different domain based on stdId value
if stdId=1000 request has to be redirected to http://localhost:50743/api/Values?stdId=1000&stdName="Test"
if stdId=1001 request has to be redirected to
http://localhost:50752/api/Values?stdId=1001&stdName="Test1"
if stdId=1002 request has to be redirected to
http://localhost:50753/api/Values?stdId=1002&stdName="Test1"
etc..,
Code what i have tried:
<rewrite>
<rules>
<rule name="Redirect to another service" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" negate="false" pattern="(stdId=1000)" />
</conditions>
<action type="Redirect" url="http://localhost:50762/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
You could use below rule to redirect url based on query string:
<rule name="querystring redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="stdId=1000" />
</conditions>
<action type="Redirect" url="http://localhost:50743/api/Values?stdId=1000&stdName="Test"" appendQueryString="false" />
</rule>
Create a rule for every URL query string parameter.
Regards,
Jalpa.

Double escaping along with URL rewrite in IIS

I have a URL(auto-generated) which is having plus( + ) in the path and I was getting 404 for that.
I searched for a while a found that we can enable double encoding using the following:
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
The problem I am facing is with the rewrite rule for that URL, the plus is being replaced with whitespace in the captured groups:
<rule name="Test Page Rewrite" stopProcessing="true">
<match url="^test/([\-a-z0-9_.+]+)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="test/index.cfm?p1={R:1}" />
</rule>
e.g.,
For URL: /test/test+page
The URL parameter available on the page is p1: test page
Is there any workaround for this to capture from the requested URL so that the URL parameter p1 will have a value of test+page(original)?
You can use UrlEncode function. This rule should work for you:
<rule name="Test Page Rewrite" stopProcessing="true">
<match url="^test/([\-a-z0-9_.+]+)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/test/index.cfm" negate="true" />
</conditions>
<action type="Rewrite" url="test/index.cfm?p1={UrlEncode:{R:1}}" />
</rule>

can anyone tell me whats wrong with this IIS URL rewrite?

So i should be able to visit rsstest/test.xml and be forwarded to article.xml if i don't have a user agent of wibble.
why doesn't this work?
<rule name="df" patternSyntax="ExactMatch" stopProcessing="true">
<match url="^rsstest/test.xml" negate="true" />
<conditions>
<add input="{HTTP_USER_AGENT}" pattern="Wibble" negate="true" />
</conditions>
<action type="Rewrite" url="article.xml" />
</rule>
thanks in advance
Try changing the rule to this.
<rule name="df" patternSyntax="ExactMatch" stopProcessing="true">
<match url="^rsstest/test.xml" negate="true" />
<conditions>
<add input="{HTTP_USER_AGENT}" pattern="Wibble" negate="true" />
</conditions>
<action type="Redirect" url="article.xml" />
</rule>
Using redirect sends a HTTP 301 or 302 to the client, telling the client that it should try to access the page using another URL.
Rewriting happens on the server, and simply is a translation of one URL to another, that is used by your web application. The client will not see any change.

IIS URL Rewrite module: redirect based on query string

I Have some problems with redirecting to another URL based on the query string parameters.
I have url for mapping service, for example "http://www.mydomain.com/?lon=111&lat=222&zoom=3"
and I need it to be redirected to "http://www.mydomain.com/111/222/3".
I have this rule in my web.config, but it does not work
<rule name="Location redirect">
<match url="\?lon=(\d+)&lat=(\d+)&zoom=(\d+)" />
<action type="Redirect" url="{R:1}/{R:2}/{R:3}" redirectType="Found" />
</rule>
The reason is that URL does not include Query String, you ned to use conditions for that. If you use the User Interface, it includes a friendly URL template that will generate it for you.
Below you will find the Rewrite rule as well as the redirect so that legacy traffic (ugly URL) is redirected to the pretty URL:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^lon=([^=&]+)&lat=([^=&]+)&zoom=([^=&]+)$" />
</conditions>
<action type="Redirect" url="/{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^/([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="?lon={R:1}&lat={R:2}&zoom={R:3}" />
</rule>

Resources