Azure Server Redirect module using get parameters - azure

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

Related

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.

URL Rewrite back reference action URL not working

I have setup a url rewrite rule to redirect from one domain to another in case matching the condition. i have a site abc.aaa.com which should redirect to abc.bbb.com if url matches with *.aaa.com. When I hard code the action URL its working fine but using back reference its not working.
I am using IIS 8.5
Below are the rules.
This is not working. When I am doing this URL is showing http://abc.aaa.com/abc.bbb.com
<rule name="Redirect aaa.com" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="(.*).aaa.com(.*)$" />
</conditions>
<action type="Redirect" url="{C:1}.bbb.com{C:2}" appendQueryString="false" />
</rule>
This is working
<rule name="Redirect aaa.com" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="(.*).aaa.com(.*)$" />
</conditions>
<action type="Redirect" url="http://abc.bbb.com" appendQueryString="false" />
</rule>
I have tried the same pattern in rule pattern and same action without condition. thats also not working
<rule name="Redirect aaa.com" enabled="true" stopProcessing="true">
<match url="(.*).aaa.com(.*)$" />
<action type="Redirect" url="http://abc.bbb.com" appendQueryString="false" />
</rule>
I have found that {C:1} doesn't contain http:// part and seperated that in URL action . it's working fine.Below is the modified rule
Thanks Lex Li for the quick response
<rule name="Redirect aaa.com" enabled="true" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="(.*).aaa.com(.*)$" />
</conditions>
<action type="Redirect" url="http://{C:1}.bbb.com{PATH_INFO}" appendQueryString="true" />
</rule>

URL Redirect web.config

I have the following URL
https://www.abcsite.com/?data=w35VTqIaVXWfi4GmESj8EGCG1cdF2aT%2BUF3D?utm_source=external%20system&utm_campaign=external%20system&utm_medium=EX.com&utm_content=MMB
which has extra ? at the end. which I Would like to remove and replace with "&"
I am using following rule but it is not working. can you review this and tell me what I am doing wrong.
<rewrite>
<rules>
<rule name="fixdata" stopProcessing="true">
<match url="\?(.*)\?(.*)$" ignoreCase="false" />
<action type="Redirect" redirectType="Permanent" url="http://www.example.com/{R:1}&{R:2}" />
</rule>
</rules>
</rewrite>
Thanks.
You rule should be like this:
<rule name="fixdata" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="(.*)\?utm_source(.*)$" />
</conditions>
<action type="Redirect" url="{R:0}?{C:1}&utm_source{C:2}" appendQueryString="False" />
</rule>
Your rule didnt work, because <match url= contains path without query string. You need to create additional condition to match query string with your regexp pattern

Url Rewrite Module Not Working When Browsed Too

Why isn't this rule working when I go to a browser with the URL rewrite module?
It works on the regex tester with the url rewrite module.
I even put it at the top of all my rules.
Example url: organizations/51/middle-tennessee-basketball-showcases-basketball-tournaments?page=1
Rewrite rule:
<rule name="Organization Redirect" stopProcessing="true">
<match url="^organizations/(.*)-basketball-tournaments\?page=1$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="organizations/{R:1}" appendQueryString="false" />
</rule>
Your rule should be as following:
<rule name="Organization Redirect" stopProcessing="true">
<match url="^organizations/(.*)-basketball-tournaments$" />
<conditions>
<add input="{QUERY_STRING}" pattern="^page=1$" />
</conditions>
<action type="Redirect" url="organizations/{R:1}" appendQueryString="false" />
</rule>
You should not check the query string (here page=1) in the url test but in the conditions section.

IIS 7 URL Rewrite match for particular URL

I would like to match exactly https://internet.dev.local/KYR url (without / into end) and redirect or rewrite to https://internet.dev.local/KYR/ (with /).
I am trying the following rule but it matches other URLs as well e.g. https://internet.dev.local/KYR/Admin/Form/Default.aspx?signup=false, which is wrong.
so how can I achieve this?
<rule name="Static redirect" patternSyntax="ExactMatch" stopProcessing="true">
<match url="https://internet.dev.local/KYR" negate="true" />
<conditions>
</conditions>
<action type="Redirect" url="/Login/?ReturnUrl=/Member/KYR/" redirectType="Permanent" />
</rule>
If you need to test the host and protocol you have to put it in the conditions, not in the global rule.
Following your example, it would be as follow:
<rule name="test" patternSyntax="ExactMatch" stopProcessing="true">
<match url="KYR" />
<action type="Redirect" url="/KYR/" redirectType="Permanent" />
<conditions>
<add input="{HTTP_HOST}" pattern="internet.dev.local" />
<add input="{HTTPS}" pattern="on" />
</conditions>
</rule>
I have changed the url in the action because your question says:
redirect or rewrite to https://internet.dev.local/KYR/ (with /).
EDIT:
To get it to work on any host (with or without SSL), just remove the conditions:
<rule name="test" patternSyntax="ExactMatch" stopProcessing="true">
<match url="KYR" />
<action type="Redirect" url="/KYR/" redirectType="Permanent" />
</rule>

Resources