web.config rule to match a parent page and any descendents - asp.net

I'm not familiar with asp.net but need to add some redirects to the web.config. I added some examples of what I'm looking to match and my attempt of creating a rule:
<!--
URLs to match:
- example.com/parent-page
- example.com/parent-page/child
- example.com/parent-page/child/grandchild
-->
<rule name="parent-page/*" stopProcessing="true">
<match url="^parent-page/(.*)"/>
<action redirectType="Permanent" type="Redirect" url="/new-parent-page/{R:1}"/>
</rule>
I don't have the environment setup to test this at the moment so I'm wondering if this looks right?

I think this is the correct rule:
<rule name="parent-page/*" stopProcessing="true">
<match url="^parent-page(.*)"/>
<action type="Rewrite" url="new-parent-page{R:1}" appendQueryString="true"/>
</rule>

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.

IIS Rewrite - match URL overwriting

I'm trying to set up some 301 redirects however I'm running into trouble with the match URL statement.
This is my IISUrlRewrite.xml file:
<rewrite>
<rules>
<rule name="CAS to PAS" stopProcessing="true">
<match url="/call-answering-service" />
<action type="Redirect" url="/us/phone-answering-service" redirectType="Permanent" appendQueryString="true" />
</rule>
<rule name="PAS tp CAS lp" stopProcessing="true">
<match url="/us/lp/phone-answering-service" />
<action type="Redirect" url="/us/lp/call-answering-service" redirectType="Permanent" appendQueryString="true" />
</rule>
</rules>
</rewrite>
So the first redirect lives at https://localhost:5001/us/call-answering-service/
which should redirect to https://localhost:5001/us/call-answering-service/
this one works fine, however, the 2nd redirect which lives at
https://localhost:5001/us/lp/phone-answering-service/ redirects to https://localhost:5001/us/phone-answering-service instead of https://localhost:5001/us/lp/call-answering-service/
I'm assuming this is causing the first rule it matching the URL contains /call-answering-service after it gets redirected by the second rule and hits the first rule. is there a way for me to make this more explicit?
Thanks

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>

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.

Outbound IIS Rewrite Rule not adding Parameter, what am I missing?

I want to use IIS Rewrite to change /blog.aspx?Page=2 to /blog?Page=2 but as its the address is in a DataPager, its not so simple. TO get round this, I've come up with the following IIS rewrite rule.
<outboundRules>
<rule name="BlogArticlesOutbound" stopProcessing="true">
<match filterByTags="A" pattern="^/blog.aspx\?page=([0-9]+)" />
<action type="Rewrite" value="/blog?page={R1}" />
</rule>
</outboundRules>
However, the output is missing the page number, what do I need to do to change it to get it to work? My style sheet seems to also disappear from the page when i use the rule.
Your back-reference syntax is a tiny bit wrong. You should use it as {R:1}.
<outboundRules>
<rule name="BlogArticlesOutbound" stopProcessing="true">
<match filterByTags="A" pattern="^/blog.aspx\?page=([0-9]+)" />
<action type="Rewrite" value="/blog?page={R:1}" />
</rule>
</outboundRules>

Resources