iis rewrites rules matching parameters - asp.net

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

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 / web.config url redirect rule based on regex is not triggered

Here is my web.config rule
<rule name="spiderRedirect" stopProcessing="true">
<match url=".post/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z-]+)" />
<action type="Redirect" url="https://example.net/bot/post.php?category={toLower:{R:1}}&id={toLower:{R:2}}&title={toLower:{R:3}}" appendQueryString="false" />
</rule>
This should give this result
input:
https://example.net/post/celebrity/4cHYQ7i/maisie-williams
should result in:
{R1}: celebrity
{R2}: 4cHYQ7i
{R3}: maisie-williams
and it should redirect to:
https://example.net/bot/post.php?category=celebrity&id=4cHYQ7i&title=maisie-williams
This input:
https://example.net/post/nerdy/NE5cHQZ/when-i-have-to-do-technical-support
should result in:
{R1}: nerdy
{R2}: NE5cHQZ
{R3}: when-i-have-to-do-technical-support
The server runs and I am quite sure the regex is correct, but the rule is never triggered. I am never redirected to google, even if I use the above input.
Why isn't the rule triggered?
I got it working. Also with check for User agent.
I use this to redirect web crawlers.
<rule name="spiderRedirect" stopProcessing="true">
<match url=".?post/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z-]+)" />
<conditions>
<add input="{HTTP_USER_AGENT}" pattern="(Google|MSNBot|Twitterbot|Pinterest|Facebot|facebookexternalhit|MJ12bot|bingbot|SimplePie|SiteLockSpider|okhttp|curl|YandexBot|ScoutJet|Slurp|DuckDuckBot|Baiduspider|Sogou|Konqueror|Exabot|ia_archiver|Screaming)"/>
<add input="{HTTPS}" pattern="on"/>
</conditions>
<action type="Redirect" url="https://example.net/bot/post.php?category={toLower:{R:1}}&id={toLower:{R:2}}&title={toLower:{R:3}}" appendQueryString="false" />
</rule>
You will escape "/" to matching your different input. The new regex is :
([0-9a-zA-Z]+)\/([0-9a-zA-Z]+)\/([0-9a-zA-Z-]+)
You can test it on https://regex101.com.
It match for your two different input.

How to check condition in url rewrite

I have a rule in url rewrite module like below
<rule name="articleSectionUrl">
<match url="(.+)/([0-9]+)/(.+)/([0-9]+)" />
<action type="Rewrite" url="article.aspx?articleid={R:4}" appendQueryString="false" />
</rule>
It works properly as it detects below type of article url's:
http://www.emusician.com/news/0766/spl-launches-usb-and-madi-interfaces/151461
That's fine, but it also detects below type of url, which is the image path unfortunately:
http://www.emusician.com/Portals/9/SlideShowThumbnails/15/ad300x250_02.gif
Can anyone tell me how can i avoid this.
Thanks all, #Aquillo as per your regex it will just limit the file whose extension I provide in the regex, that means I always have to provide the extensions in the regex for the url I want to ignore, so here's the complete solution to my problem.
UrlRewrite module has a <conditions> element under <rule> with this we limit any type of file which comes in the url.
Here's the complete solution:
<rule name="articleSectionUrl">
<match url="(.+)/([0-9]+)/(.+)/([0-9]+)" />
<action type="Rewrite" url="article.aspx?articleid={R:4}" appendQueryString="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
</rule>
Thanks guys for helping me.

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>

Resources