Rewrite Rule to Rewrite All EXCEPT File Extension - iis-7

I've got a rule setup to rewrite everything going into a subdirectory like so:
<rule name="Forms Directory" stopProcessing="true">
<match url="^forms/(.*)" />
<action type="Redirect" url="forms.htm" redirectType="Permanent" />
</rule>
However, I want to make a slight change to allow it to access an ASP file in the forms folder. So I want to keep the same rule but exclude any .asp from matching the rule. I tried the following but couldn't get it to operate as expected:
<rule name="Forms Directory" stopProcessing="true">
<match url="^forms/(.*)[^(.asp)]" />
<action type="Redirect" url="forms.htm" redirectType="Permanent" />
</rule>
Any help on this would be greatly appreciated!

An additional condition that checks file extension solves this.
<rule name="Forms Directory">
<match url="^forms/(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern=".+\.asp$" negate="true" />
</conditions>
<action type="Rewrite" url="forms.htm" redirectType="Permanent" />
</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.

Why isn't this image URL Rewrite rule working?

I have written a URL rewrite to fix images paths to move to a new location for images however it isn't working.
<rule name="Artist Images" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^/images/$" />
</conditions>
<action type="Rewrite" url="https://myserver.blob.core.windows.net/v2/images/" redirectType="Permanent" />
</rule>
The expected behavior is that when a request for an image comes in (http://myserver.com/images/sample.jpg) that the new image location
(https://myserver.blob.core.windows.net/v2/images/sample.jpg) is used.
It is a .net application hosted on Azure. Rule is in the web.config.
Any ideas?
Because your condition is not correct, you are putting sign $ which means end of the string, but this is not correct.
<rule name="Artist Images" stopProcessing="true">
<match url="^images/(.*)$" />
<action type="Rewrite" url="https://myserver.blob.core.windows.net/v2/images/{R:1}" />
</rule>
This is what ended up working but Alexey's answer put us in the right direction.
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_URL}" pattern="/images/(.*)" />
</conditions>
<action type="Redirect" url="https://artistshare.blob.core.windows.net/images/{C:1}" />
</rule>

IIS/ASP Redirect a domain to another, both having the same source

I have to redirect a domain to another, tricky part being the new domain uses the source of the old one as a host. Implicitly, if I did a general redirect, it would loop and 500 Internal Error.
Grabbed my .htaccess guns to get ready for war only to find that the server is not linux and it uses asp. I've read a bit, accessed the web.config file and the default ruleset is this (which I assume is pretty much standard-issue)
<rewrite>
<rules>
<rule name="Default Document" stopProcessing="true">
<match url="(.*)default" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Is there any way I can do a conditional that checks the domain name and only if the rule matches, it redirects? Something like:
<rules>
<rule name="Redirect" stopProcessing="true">
<match url="(.*)oldDomain.com" />
<action type="Redirect" url="(.*)newdomain.com$" redirectType="Permanent"/>
</rule>
</rules>
// Actual domain and testing
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="(.*)intermedcasasibiu\.ro$" />
</conditions>
<action type="Redirect" url="{C:1}intermedcasasibiu.ro/{R:1}" redirectType="Permanent" />
</rule>
Which outputs :
http://intermedcasa.ro/intermedcasasibiu.ro/intermedcasasibiu.ro/intermedcasasibiu.ro/intermedcasasibiu.ro/intermedcasasibiu.ro/AndSoOn
Yes, it's possible:
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="(.*)newdomain\.com$" />
</conditions>
<action type="Redirect" url="{C:1}newdomain.com/{R:1}" redirectType="Permanent" />
</rule>
Apart from that, If you are more familiar with htaccess rewrite rules, you can import them into IIS, check this guide

IIS rewrite rule doesn't work

I want to simply to redirect Radio/Play?stationId=124 to station/124 but below configuration doesn't work. Other rules in my configuration works so rewrite modules working.
<rule name="radio-play" stopProcessing="true">
<match url="^Radio/Play?stationId=([0-9]+)" ignoreCase="true" />
<action type="Redirect" url="station/{R:1}" redirectType="Permanent" />
</rule>
What is wrong in the configuration ?
Handling query string parameter is different. Here is correct way.
<rule name="radio-play" stopProcessing="true">
<match url="^radio/play" ignoreCase="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="stationid=(\d+)" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="istasyon/{C:1}" redirectType="Permanent" appendQueryString="false"/>
</rule>

Conditional query string redirect

I'm trying to redirect requests that have a query string to a different domain name.
I've got a short url, http://short.url and I want to redirect http://short.url?hello to http://long.url/?hello. So the query string has to be kept by the redirect. To make things more complicated, I would like to rediect http://short.url?hello,hello2 to http://long.url/advanced.aspx/?hello,hello2.
Here is the rule I've got now (only dealing with the first part of my question)
<rewrite>
<rules>
<rule name="test" patternSyntax="ECMAScript" stopProcessing="true">
<match url="~/?\w+" />
<action type="Redirect" url="http://long.url/?{R:0}" redirectType="Found" />
</rule>
</rules>
</rewrite>
However, I am not seeing any redirects. Also, is there a better way to do this? Basically I just want to setup a shortcut to pass queries to a website. These are not meant to be permanent so I'm using redirectType="Found".
In case anyone is looking to do this:
<rules>
<rule name="Basic" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\w*" />
<action type="Redirect" url="http://longurl.com" redirectType="Found" />
<conditions>
<add input="{QUERY_STRING}" pattern="\w+" />
<add input="{QUERY_STRING}" pattern="\," negate="true" />
</conditions>
</rule>
<rule name="Advanced" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\w*" />
<action type="Redirect" url="http://longurl.com/advanced.aspx" redirectType="Found" />
<conditions>
<add input="{QUERY_STRING}" pattern="^\w+(?=\,)" />
</conditions>
</rule>
</rules>

Resources