I am trying to redirect http://example.com/auth/recover?u=123&t=456 to http://example.com/#!/passwordreset/123/456
My URL config looks like.
<rewrite>
<rules>
<rule name="Redirect Forgotten Password" stopProcessing="true">
<match url="^auth/recover" />
<conditions>
<add input="{QUERY_STRING}" pattern="u=([0-9]+)" />
<add input="{QUERY_STRING}" pattern="t=([0-9]+)" />
</conditions>
<action type="Redirect" url="/#!/resetpassword/{C:0}/{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
Unfortunately when the rewrite happens I end up with a URL that looks like : http://example.com/#!/resetpassword/u=123/123
If I swap the conditions around, I get a similar thing happening (I get a t=456 in the URL).
I've found the reason it doesn't work, and a way around it so I'm going to answer my own answer here.
It seems that when using conditions in IIS rewrites, only the LAST condition will be passed to the redirect action. It also seems like {C:0} will match the entire condition, and then the indexes after that are the matching expressions within that condition.
One way would be to create a single condition that would match the query string all in one lot. But that means that your params in your query string would have to be in the same order everytime.
Instead I did the following :
<rewrite>
<rules>
<rule name="Redirect Forgotten Password" stopProcessing="true">
<match url="^auth/recover" />
<conditions>
<add input="{QUERY_STRING}" pattern="u=([0-9]+)" />
<add input="##{C:1}##_{QUERY_STRING}" pattern="##([^#]+)##_.*t=([0-9]+)" />
</conditions>
<action type="Redirect" url="/#!/resetpassword/{C:1}/{C:2}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
So it matches the first condition I want, then passes it to the next condition to be "rematched" again along with my second param. Not that elegant, but it works!
Related
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 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>
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
How can I redirect '/path?foo=abc&bar=def' to '/path/abc/def'?
I've tried the following:
<rule name="My rule" stopProcessing="false">
<match url="^path" />
<conditions>
<add input="{QUERY_STRING}" pattern="foo=/[^&]*/" />
<add input="{QUERY_STRING}" pattern="bar=(.*)" />
</conditions>
<action type="Redirect" url="/path/{C:0}/{C:1}" appendQueryString="false" />
</rule>
But this doesn't even process my match. Where am I going wrong?
I don't have access to a computer to verify this, but I think the following could simplify:
<rule name="Redirect old FAQ view" stopProcessing="false">
<match url="^path.*" />
<conditions>
<add input="{QUERY_STRING}" pattern="^foo=([^&]*).*?bar=(.*)" />
</conditions>
<action type="Redirect" url="/path/{C:0}/{C:1}" appendQueryString="false" />
</rule>
You don't need the second input line as you can use multiple capturing groups in one. I'm not sure if the whole line will come back as a match or not so you may need to adjust to {C:1}/{C:2}. Finally, I'm not sure if the match url was problematic. It would contain the query string as well, so that may have been causing it to miss. Look here for more details on the various components of the URL rewrite process.