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

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.

Related

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 rewrites rules matching parameters

I'm trying to write a rules that match fr/Catalogue/?cf=jupes and redirect to vetements-femme/jupes/
I wrote the following rules :
<rule name="PapJupesSansGenre">
<match url="^fr\/Catalogue\/\?cf=jupes$"/>
<action type="Redirect" url="vetements-femme/jupes/"/>
</rule>
but it's not working. I guess the trouble is coming from the ? because if I try ^fr\/Catalogue\/cf=jupes$ it's working fine with fr/Catalogue/cf=jupes.
Any suggestions?
url doesn't include the querystring part, you should add a condition to match the url and the querystring.
The following rule should work :
<rule name="PapJupesSansGenre" >
<match url="^/fr/Catalogue/$" />
<conditions>
<add input="{QUERY_STRING}" pattern="cf=jupes" />
</conditions>
<action type="Redirect" url="vetements-femme/jupes/" appendQueryString="false"/>
</rule>
The appendQueryString attribute will indicate IIS not to vetements-femme/jupes/?cf=jupes

Translating apache rewrite rules to IIS web.config

I am trying to translate this apache rewrite rule into web.config rules but I can't get it to work.
Basically it checks the user agent and redirect the agent to the url provided
# allow social media crawlers to work by redirecting them to a server-rendered static version on the page
RewriteCond %{HTTP_USER_AGENT (facebookexternalhit/[09]|Twitterbot|Pinterest|Google.*snippet)
RewriteRule qs/(\d*)$ http://sitetocrawl.com/doc?id=$1 [P]
This is what I have so far. However, I can't figure out how to catch the url querystring parameter. Basically the text string after http://example.com/qs/parameter
<rule name="Social Rewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="urltomatchpattern" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_USER_AGENT}" pattern="facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet" />
</conditions>
<action type="Redirect" url="http://sitetocrawl.com/doc?parameter" appendQueryString="true" redirectType="Found" />
</rule>
EDIT:
I tried with many variants of simpler rules, like redirect/rewrite when a specific user agent requests the site(in my case, the facebook crawler). But I can't even get those rules to work. I am debugging using the Facebook OG debugger
<rule name="Rule1" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_USER_AGENT}" pattern="facebookexternalhit/1.1|Facebot" />
</conditions>
<action type="Redirect" url="new url here" />
</rule>
Not an answer but a starting point. The IIS Manager (IIS 8 on Windows 8.1) translates your apache mod_rewrite rules into this slightly different configuration:
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="qs/(\d*)$" ignoreCase="false" />
<conditions>
<add input="%{HTTP_USER_AGENT}" pattern="(facebookexternalhit/[09]|Twitterbot|Pinterest|Google.*snippet)" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="http://sitetocrawl.com/doc?id={R:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I see that it is rewrite instead of redirect, but please check if this would work for your scenario. And if it works, you may begin changing it until reaching desired result.
And now I see that Your main URL Matching pattern is simply urlmatchpattern which of course is not a pattern and is the root cause for your rules to not work.

IIS Url-Rewrite

I'm trying to use IIS7 Url Rewrite module rewrite
services.mydomain.com/some-file-here to mydomain.webhost.com/folder/some-file-here
The rule is as follows:
Pattern = ^services.mydomain.com/(.*)$
Action = Rewrite
Rewrite URL = http://mydomain.webhost.com/folder/{R:1}
The problem is IIS keeps giving me 404 not found errors. I've been stuck at this for days now. Any ideas?
You have your pattern wrong. It should not include domain name or query string there -- only path without leading slash. See the working rule below:
<rule name="MyRewriteRule" stopProcessing="true">
<match url="^(some-file-here)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^services\.mydomain\.com$" />
</conditions>
<action type="Redirect" url="http://mydomain.webhost.com/folder/{R:1}" />
</rule>
The above rule will only trigger if host name is services.mydomain.com. If you do not require such additional condition (which is optional) then just remove these 3 lines: <conditions>...</conditions>
Also, the above rule will only do one specific redirect from services.mydomain.com/some-file-here to mydomain.webhost.com/folder/some-file-here. If you need to redirect ANY file like that, then use this one instead:
<rule name="MyRewriteRule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^services\.mydomain\.com$" />
</conditions>
<action type="Redirect" url="http://mydomain.webhost.com/folder/{R:1}" />
</rule>

How to rewrite url with IIS7 Rewriter and question mark

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.

Resources