URL rewrite Pattern in iis 10 - iis

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>

Related

How to add rules in Url redirect in iis 10

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.

URL Rewriting the path in IIS

I have a site that uses two characters in the URL path to determine the initial language e.g. https://my.company.net/Monitoring/gb displays in the English language. I want to setup a redirect for these two display the full culture code e.g. https://my.company.net/Monitoring/en-GB
This is the rule that I've tried:
<rewrite>
<rules>
<rule name="Monitoring gb rewrite" patternSyntax="ExactMatch" stopProcessing="true">
<match url="https://my.company.net/Monitoring/gb" />
<conditions />
<serverVariables />
<action type="Redirect" url="https://my.company.net/Monitoring/en-GB" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I expected https://my.company.net/Monitoring/gb to redirect to https://my.company.net/Monitoring/en-GB however this rule does not have any effect: the browser URL stays at https://my.company.net/Monitoring/gb.
How can I rectify this?
You could use below url rewrite rule.
<rule name="gb to en-gb redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="ww.sample1.com" />
<add input="{HTTPS}" pattern="on" />
<add input="{REQUEST_URI}" pattern="Monitoring/gb" />
</conditions>
<action type="Redirect" url="https://www.sample1.com/Monitoring/en-GB" />
</rule>
Note: use your hostname instead of the www.sample1.com.
The redirect failure was due to browser caching. The redirect works once the cache is cleared.

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.

Azure Server Redirect module using get parameters

I try to make a permanent redirection on an azure website using the rewriting module using this pattern :
<rule name="Rewrite redirect-not-found-products-w" patternSyntax="ExactMatch" stopProcessing="true">
<match url="product/Product.aspx?product_id=287"/>
<action type="Redirect" url="https://example.com/product" redirectType="Permanent"/>
</rule>
But I would like take care of GET parameters
By example redirect :
example.com/product/Product.aspx?product_id=287
or
example.com/product/Product.aspx?product_id=35
to
example.com/product
but not the whole product/Product.aspx
Your rule should have condition with query string, you cannot use query string in match url=
<rule name="Rewrite redirect-not-found-products-w" stopProcessing="true">
<match url="^product/Product.aspx$" />
<conditions logicalGrouping="MatchAny">
<add input="{QUERY_STRING}" pattern="^product_id=287$" />
<add input="{QUERY_STRING}" pattern="^product_id=35$" />
</conditions>
<action type="Redirect" url="https://example.com/product" redirectType="Permanent" />
</rule>
Rule above will redirect:
example.com/product/Product.aspx?product_id=287
or
example.com/product/Product.aspx?product_id=35
to
example.com/product

IIS: Redirect subdomain to specific url

In IIS 8, I want to redirect the url http://test.example.com to http://www.example.com/abc/123
I try this, but not work.
<rule name="test" stopProcessing="true">
<match url="^test.example.com$" />
<action type="Redirect" url="http://www.example.com/abc/123" />
</rule>
you could add the pattern like this
<rule name="RedirectDomain" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="(.*)test.example.com />
</conditions>
<action type="Redirect" url="http://www.example.com/abc/123" redirectType="Permanent" />
</rule>
In the IIS GUI on the given side you should be able to choose 'HTTP Redirect' from there you can type in a url to redirect the site to.
I don't know if this approach is the recommended (It is normally used to redirect HTTP request on a given site to use HTTPS), but it will solve your problem.

Resources