How to rewrite url with IIS7 Rewriter and question mark - iis-7

I have an url like this :
mydomain/page?param1=1
and I want rewrite this to :
mydomain/page2?param1=1
<rule name="MyRule" stopProcessing="true">
<match url="page?(.*)" />
<action type="Redirect" url="page2?{R:1}" />
</rule>
or
<rule name="MyRule" stopProcessing="true">
<match url="page\?(.*)" />
<action type="Redirect" url="page2?{R:1}" />
</rule>
This does not match, I do not understand why

The ? marks the start of the query string, which is not part of the path.
So if I follow, the rewriter wont even get to see the ? and anything past it.

Related

IIS rewrite URL in asp.net goes to into infinity loop

I just need to create a rule in my web config file to rewrite all URLs to given into rule
ex:
url/services/presentations-evenementielles to
url/Services/Presentations-Evenementielles
This is all made for SEO purposes to avoid duplicates.
<rule name="ProductionRule2" stopProcessing="true" patternSyntax="ExactMatch" >
<match url="^/productions/xyz" ignoreCase="true" />
<action type="Redirect" url="Productions/XYZ" redirectType="Permanent"/>
</rule>
Above code its gives me infinite loop error.
There was some mistakes with your rule
1.You were trying to use "^" in an exact match rule. Please set it to regular expression
2.You are trying to use "/" in match url. The match url part of domain/productions/xyz is
productions/xyz instead of /production/xyz.
3.You are enabling ignore case when you redirect URL to itself.
So please try to modify the rule like this
<rule name="ProductionRule2" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^productions/xyz" ignoreCase="false" />
<action type="Redirect" url="Productions/XYZ" redirectType="Permanent" />
</rule>
Update:
Please modify the rule like this
<rule name="ProductionRule2" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^Productions/XYZ" ignoreCase="false" negate="true" />
<action type="Redirect" url="Productions/XYZ" redirectType="Permanent" />
<conditions>
<add input="{ToLower:{URL}}" pattern="/productions/xyz" />
</conditions>
</rule>
And this is working on my side
Please remember to clean browser cache when you test the rule.And the URL should be prODuctions/Xyz instead of prODuction/Xyz.

URL rewriting to another sub domain

I am trying to create an URL Rewrite rule to send :
www.mycompany.com/file.aspx?someQueryString
or
www.mycompany.com/fileXYZ.aspx?someQueryString
to
www.subdomain.mycompany.com/file.aspx?someQueryString
or
www.subdomain.mycompany.com/fileXYZ.aspx?someQueryString
I wrote this rule:
<rule name="MyPageRewrite" stopProcessing="true">
<match url="(.*)(file[a-zA-Z]*\.aspx)" />
<action type="Rewrite" url="http://subdomain.mycompany.com/{R:1}" />
</rule>
The match expression seems to work, however the destination doesn't? What is wrong?
Thanks Lex Li, I did many tests with the test aspx file you provided and found a rule that works:
<rule name="MyPageRewrite" stopProcessing="true">
<match url="(.*)(file[a-zA-Z]*\.aspx)" />
<action type="Redirect" redirectType="Found" url="http://subdomain.mycompany.com/{R:0}" appendQueryString="true"/>
</rule>

iis rewrite keep path (keep everything but the file name)

attempting to re route
https://xxxxx.aaa.bb.cc.dd/FOB_Main.aspx?FOB=O to
https://xxxxx.aaa.bb.cc.dd/FOB_Main_Rewrite.aspx?FOB=O
and
https://xxxxx.aaa.bb.cc.dd/testdev/FOB_Main.aspx?FOB=O to
https://xxxxx.aaa.bb.cc.dd/testdev/FOB_Main_Rewrite.aspx?FOB=O
and
https://xxxxx.aaa.bb.cc.dd/net/prodmirror/FOB_Main.aspx?FOB=O to
https://xxxxx.aaa.bb.cc.dd/net/prodmirror/FOB_Main_Rewrite.aspx?FOB=O
so retain everything before an aspx page and the query string. and replace it with a new aspx page. (replace only the filename and keep everything else)
here is my attempt.
<rule name="Fob_Main_decommisioned" stopProcessing="true">
<match url="^fob_main.aspx" />
<action type="Redirect" url="/FOB_Main_Rewrite.aspx" redirectType="Temporary" />
</rule>
this is working fine for
https://xxxxx.aaa.bb.cc.dd/FOB_Main.aspx?FOB=O to
https://xxxxx.aaa.bb.cc.dd/FOB_Main_Rewrite.aspx?FOB=O
but it is changing.
https://xxxxx.aaa.bb.cc.dd/testdev/FOB_Main.aspx?FOB=O to
https://xxxxx.aaa.bb.cc.dd/FOB_Main_Rewrite.aspx?FOB=O
I also attempted
<rule name="Fob_Main_decommisioned" stopProcessing="true">
<match url="^fob_main.aspx" />
<action type="Redirect" url="{HTTP_HOST}/FOB_Main_Rewrite.aspx" redirectType="Temporary" />
</rule>
but now I am getting
https://xxxxx.aaa.bb.cc.dd/testdev/xxxxx.aaa.bb.cc.dd/FOB_Main_Rewrite.aspx?FOB=O
I also attempted
<rule name="Fob_Main_decommisioned" stopProcessing="true">
<match url="(^.*)fob_main.aspx" />
<action type="Redirect" url="/{R:1}FOB_Main_Rewrite.aspx" redirectType="Temporary" />
</rule>
but I am back to
https://xxxxx.aaa.bb.cc.dd/FOB_Main_Rewrite.aspx?FOB=O
(it is removing the testdev again)
interestingly using this rule
<rule name="Fob_Main_decommisioned" stopProcessing="true">
<match url="(^.*)fob_main.aspx" />
<action type="Redirect" url="/{R:1}FOB_Main_Rewrite.aspx" redirectType="Temporary" />
and directly typing into the browser
https://xxxxx.aaa.bb.cc.dd/testdev/testdev/FOB_Main.aspx?FOB=O
re routes me to the page I would like to go to.
https://xxxxx.aaa.bb.cc.dd/testdev/FOB_Main_Rewrite.aspx?FOB=O
This rule looks correct (if you placed this rule in root application):
<rule name="Fob_Main_decommisioned" stopProcessing="true">
<match url="(^.*)fob_main.aspx" />
<action type="Redirect" url="/{R:1}FOB_Main_Rewrite.aspx" redirectType="Temporary" />
</rule>
This rule will work, if you placed it in sub application:
<rule name="Fob_Main_decommisioned" stopProcessing="true">
<match url="^fob_main.aspx" />
<action type="Redirect" url="FOB_Main_Rewrite.aspx" redirectType="Temporary" />
</rule>
Most probably you back to https://xxxxx.aaa.bb.cc.dd/FOB_Main_Rewrite.aspx?FOB=O, because browser cached your wrong redirect before. Can you please do this steps:
Apply this rule
Clear cache in your browser
Try to open https://xxxxx.aaa.bb.cc.dd/testdev/FOB_Main.aspx?FOB=O

How to create rewrite rule that matches multiple URLs

I now have this per language, but as you can see they all rewrite to the same page:
<rule name="help en">
<match url="^help?/?$"/>
<action type="Rewrite" url="help.aspx"/>
</rule>
<rule name="help sp">
<match url="^ayuda?/?$"/>
<action type="Rewrite" url="help.aspx"/>
</rule>
OR more complex parameterized rewrites like:
<rule name="news category overview_de">
<match url="^artikelen/([a-zA-Z0-9-+']+)?/?$" />
<action type="Rewrite" url="newsoverview.aspx?category={R:1}" />
</rule>
<rule name="news category overview_en">
<match url="^articles/([a-zA-Z0-9-+']+)?/?$" />
<action type="Rewrite" url="newsoverview.aspx?category={R:1}" />
</rule>
This takes up a lot of space, so is there a way to write this shorter?
I checked out this post: How to add multiple URL rewrite rules in a web.config, but that does not make it any shorter.
Is there a way I can repeat the match url condition or add multiple urls to it?
Update 1
I tried this:
<rule name="news overview">
<match url="^(artikelen|articles)?/?$" />
<action type="Rewrite" url="newsoverview.aspx" />
</rule>
<rule name="news category overview">
<match url="^(artikelen|articles)/([a-zA-Z0-9-+']+)?/?$" />
<action type="Rewrite" url="newsoverview.aspx?category={R:1}" />
</rule>
But this now redirects even requests to my homepage to the newsoverview.aspx page.
You are using regular expression here, so you can use:
^(help|ayuda|hilfe|aide)?/?$
so you just put the language dependent word into a OR construct:
^(articles|artikelen|artikel)/([a-zA-Z0-9-+']+)?/?$
this should work as long as the parameterized part is always the same.

regex to replace one word in url - 301 Redirect using URL Rewrite IIS asp.net

I want to set up a 301 redirect for
mysite.com/banana mysite.com/fruit
mysite.com/banana/yellow mysite.com/fruit/yellow
mysite.com/banana/yellow/sale mysite.com/fruit/yellow/sale
(The words "yellow" and "sale" could be replaced with anything, they are variables in a route.)
This works for the first case, but not the others:
<rule name="banana" stopProcessing="true">
<match url="banana" />
<action type="Redirect" url="fruit" />
</rule>
Based on this question:
replace underscore with dash using url rewrite module of iis
This works for the third case only:
<rule name="banana" stopProcessing="true">
<match url="(mysite.com.*/[^/]*?)banana/([^/_]*)/([^/_]*)$" />
<action type="Redirect" url="{R:1}fruit/{R:2}/{R:3}" />
</rule>
What I need is a regex that returns
{R:1} mysite.com/
{R:2} /yellow/sale
or
{R:1} mysite.com/
{R:2}
Why not keep it simple?
<match url="mysite.com/banana(/.+)*$" />
<action type="Redirect" url="mysite.com/fruit{R:1}" />
Or if you only want to go two levels deep:
<match url="mysite.com/banana((?:/[^/]+){0,2})$" />
<action type="Redirect" url="mysite.com/fruit{R:1}" />
How about the following:
<rule name="banana" stopProcessing="true">
<match url="mysite.com/banana(.+)?" />
<action type="Redirect" url="mysite.com/fruit{R:1}" />
</rule>

Resources