IIS Rewrite - match URL overwriting - asp.net

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

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

Redirection loop when url rewriting

I'm trying to avoid having 2 different urls for my landing page so I want to redirect landing page that has no culture specified (www.example.com) to www.example.com/en-us
What I'm trying to achieve is to redirect links that ends with example.com to example.com/en-us
This is my rule in web.config:
<rule name="Redirect to En Us" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com$" />
</conditions>
<action type="Redirect" url="/en-us" redirectType="Permanent" />
</rule>
But it just stuck in a redirection loop even if the url is not ending with example.com
Update:
This doesn't fall in a loop but it doesn't redirect either (seems that regular expression doesn't match at all but why?):
<rule name="Redirect to En Us" stopProcessing="true">
<match url="example.com$" />
<action type="Redirect" url="/en-us" redirectType="Permanent" />
</rule>
Well I finally got it figured. I just had to tell that if the url (excluding the host name) is empty then redirect:
<rule name="Redirect to En Us" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/en-us" redirectType="Permanent" />
</rule>
You might need to look for a trailing slash too but in my case as I already have a rule that removes trailing slashes its not an issue.

How can I correctly rewrite a URL with .aspx to a URL without .aspx?

I have a site that currently uses the .aspx extension on its pages. It's getting a Joomla conversion and the .aspx extension will not work anymore. I need it so that if someone enters the .aspx extension, it will just get removed the URL so none of the SEO on the current site breaks.
For example, I need
www.arrc.com.php5-17.websitetestlink.com/services/managed-services.aspx
to be rewritten/redirected to
www.arrc.com.php5-17.websitetestlink.com/services/managed-services
This is what I have in my web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Migrate to PHP">
<match url="^([_0-9a-z-]+)\.aspx" />
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
<rule name="Rewrite .aspx">
<match url="^([_0-9a-z-]+)/?([_0-9a-z-]+)\.aspx" />
<action type="Redirect" redirectType="Permanent" url="{R:1}/{R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The first URL match is for any URLs that have a URL like services.aspx instead of services/managed-services.aspx
Whenever I go to www.arrc.com.php5-17.websitetestlink.com/services/managed-services.aspx it gives me an internal server error, but www.arrc.com.php5-17.websitetestlink.com/services.aspx rewrites correctly. What can I do to fix this?
They are greedy, switch the order.
<rule name="Rewrite .aspx">
<match url="^([_0-9a-z-]+)/?([_0-9a-z-]+)\.aspx" />
<action type="Redirect" redirectType="Permanent" url="{R:1}/{R:2}" />
</rule>
<rule name="Migrate to PHP">
<match url="^([_0-9a-z-]+)\.aspx" />
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
Two potential issues:
I think for rewriting, it's going to hit them in order, which means the first rule is going to always kick off. Try switching the order of the rule.
The dash should work correctly in [_0-9a-z-], but try escaping the dash.

Resources