Redirection loop when url rewriting - asp.net

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.

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 Rewrite: redirect from one page to another

I want to redirect all request for https://www.example.com/about to https://www.anotherexample.com. The following rewrite rule does not work.
<rule name="RULENAME" stopProcessing="true">
<match url="^about" ignoreCase="true" />
<action type="Redirect" url="https://www.anotherexample.com" appendQueryString="false" />
</rule>
Both sites are WordPress sites. What am I missing?
EDIT 1
I notice that if my match url is <match url="(.*)" ignoreCase="true" />, when i access example.com, it correctly redirects, but example.com/about does not

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

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>

url rewriting rule in asp.net web.config

I am using URL rewriting like the following .
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite.com$" />
</conditions>
<action type="Redirect" url="http://www.mysite.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But I want another rule that when users write www.mysite.com , they go redirected automatically to www.mysite.com/home , i can do that in url routing but it'll take extra phases so I want it in url rewriting...can someone write it for me please?? Thanks alot.

Resources