URLRewrite IIS 7 - regex

I am trying to perform a simple URLRewriting. if you visit azamsharp.com it will take to some folder browsing structure it should go to http://www.azamsharp.com/AzamSharpWebApps/Default.aspx.
I don't want to see the AzamSharpWebApps in the URL: Here is the URL Rewrite I am using:
<system.webServer>
<rewrite>
<rules>
<rule name="Virtual Director" enabled="true" stopProcessing="false">
<match url=".*" />
<conditions>
<add input="{MyDomains:{HTTP_HOST}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}{REQUEST_URI}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MyDomains">
<add key="azamsharp.com" value="/AzamSharpWebApps/default.aspx" />
<add key="www.azamsharp.com" value="/AzamSharpWebApps/default.aspx" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>

I don't know how IIS UrlRewrite works, but at a general regex level you want to replace ^(?!/AzamSharpWebApps).+ with /AzamSharpWebApps/$0
The ^ is start of string, and the (?!..) is a negative lookahead, saying "make sure the following text is not "/AzamSharpWebApps", and then the .+ matches any character until the end of the string.
In the replace side, the $0 indicates the entire captured text - so basically the regex is saying "if it doesn't already start with "/AzamSharpWebApps", prefix it with that.
(You'll need to experiment with whether you need a / before the $0 or not.)
Anyhow, looks like IIS uses {C:0} instead of $0, just to be different, so I guess it will look something like this inside the rule:
<match url="^(?!/AzamSharpWebApps).+" />
<action type="Rewrite" url="/AzamSharpWebApps{C:0}" />
But possibly it needs more than just that.

Related

Redirect bookmarked URLS via IIS Rewrite Module is not working properly even though URL pattern matches in testing

I've read so many forums and I did possibly whatever I could. My outbound rule to works in terms of rewriting the URL for SEO purposes but my Redict URL which in case the changed URLs marked in our users' bookmarks does not work.
I am using IIS 10.0.
The URL that needs changing:
http://agmodel.com/files/content/insights/publishing/e_clouds.pdf
To:
http://agmodel.com/assets/content/insights/publishing/e_clouds.pdf
So only thing I am changing is the string "files" to "assets".
Here is what I've tried:
Attempt 1:
<rule name="Redirect" stopProcessing="true">
<match url="(https?:\/\/[^\/]+)\/" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="(https?:\/\/[^\/]+)\/files\/(.*)" />
<add input="{REQUEST_URI}" pattern="^/assets" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/assets/{C:2}" appendQueryString="false" redirectType="Found" />
</rule>
I tried to make sure that the first pattern is always the domain the second pattern is files.
Attempt 2:
<rule name="assets-to-files" stopProcessing="true">
<match url="(https?:\/\/[^\/]+)\/files\/(.*)" />
<action type="Redirect" url="{R:1}/assets/{C:1}" appendQueryString="false" logRewrittenUrl="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="\/files\/(.*)" />
</conditions>
</rule>
So whenever I test whether the bookmarked old URL will change to the new one, it does not work. It gets green light during pattern match testing in IIS 10.
What am I doing wrong here?
You may use a very simple rule here:
<rule name="assets-to-files">
<match url="^files/(.*)" />
<action type="Rewrite" url="assets/{R:1}" />
</rule>
The URL you want to match is http://agmodel.com/files/content/insights/publishing/e_clouds.pdf. The url attribute in match node will receive files/content/insights/publishing/e_clouds.pdf as input, so you want
^files/(.*)
It will match files/ at the start of the string and then will capture into {R:1} any 0 or more chars other than newline.
In the action node url attribute, all you need is to specify the assets/ new path and append what you captured into {R:1}.

IIS - Configuration file is not well-formed XML

<rule name="Posts2" enabled="true">
<match url="^index.cfm\?section=latest.news&id=(.*)" />
<action type="Rewrite" url="post.php?id={R:1}" />
</rule>
Problem is with the regex - any idea at all? I can't seem to work it out. I've run it on http://www.xmlvalidation.com/ and it says
The reference to entity "id" must end with the ';' delimiter.
When I change the ampersand to & the rewrite doesn't seem to work on URLs such as /index.cfm?section=latest.news&id=14726 despite it passing the test in IIS. Even when I escape the dots and question mark it doesn't work
Try using a {QUERY_STRING} input using conditions perhaps:
<rule name="Posts2">
<match url="index\.cfm$" />
<conditions>
<add input="{QUERY_STRING}" pattern="section=(latest\.news)" />
<add input="#{C:1}#_{QUERY_STRING}" pattern="#([^#]+)#_.*id=(\d+)" />
</conditions>
<action type="rewrite" url="post.php?id={C:2}" appendQueryString="false"/>
</rule>
URL's containing query strings are usually better handled this way, although if you really prefer not going this route then make sure you have the all the proper tags included, and also try escaping the . in between latest\.news.
<rewrite>
<rules>
<rule name="Posts2">
<match url="^index\.cfm\?section=latest\.news&id=(\d+)" />
<action type="Rewrite" url="post.php?id={R:1}" />
</rule>
</rules>
</rewrite>

IIS ARR rules not working as expected in adding trailing slashes to my applications

I have a pool of applications that I want to run under a ARR server that should serve as a router for all my applications.
I have defined a set of rules that should be applied in waterfall, but something is not working the proper way.
The first rule should handle the trailing slashes, the other rules should map my applications to resolve for my internal DNS server with rewrite URL, but the problem seems to happen during the first rule.
The default behaviour is that, if I try to launch my application with http://myapp will return 404 code, if I try to run it by adding the slash (http://www.myapp/ ) everything works fine. So basically my rule should check for URL patterns without the slash: if the resource is a directory it should add the slash at the end of the Url.
So the pattern to catch the url is the following regular expression:
.*[^/]$
This should catch url without / at the end and I successfully tested it.
For every url that matches the regexp, I should check if it's a directory, and in the case I should set the trailing slash, so:
{REQUEST_FILE} -> Is a directory
But this doesn't work. I also tried to add the following rule with no success:
{REQUEST_FILE} -> Is not a file
The rule to apply is the following:
Redirect to (rewrite leads to same behaviour, too):
{R:O}/
It seems not to add the / to my urls and I don't know how to check which steps fail to succeed. The next rules basically follow this pattern:
mywebapp/* redirect to www.mydnsappaddress/{R:1}
EDIT: I add the web.config sample to show you the textual version of the rules.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Trailing Slash" enabled="false" stopProcessing="true">
<match url=".*[^/]$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" />
</conditions>
<action type="Redirect" url="{R:0}/" />
</rule>
<rule name="app1" enabled="false" patternSyntax="Wildcard">
<match url="sites/doc/*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="http://mypersonaldnsaddress/{R:0}" />
</rule>
<rule name="ASTCO portale NWS" enabled="true" patternSyntax="Wildcard">
<match url="portale/*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="http://mypersonaldnsaddress/{R:0/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I have to ask the obvious: does your example deliberately have enabled set to false?
I was able to make this work exactly as you desire: if the directory exists, add a slash at the end, if it doesn't, don't.
<rule name="Trailing Slash" enabled="true" stopProcessing="true">
<match url=".*[^/]$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
</conditions>
<action type="Redirect" url="{R:0}/" />
</rule>

IIS Redirect Regex start of string

I am trying to redirect from bsl2 to bsl2 using IIS rewrite
http://server/site/bsl2/controllertest.aspx to
http://server/site/bsl3/controllertest.aspx
This rule works fine:
<rewrite>
<rules>
<rule name="BSL" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{PATH_INFO}" pattern="bsl2/(.*)$" />
</conditions>
<action type="Redirect" url="bls3/{C:1}?test={C:0}" />
</rule>
</rules>
</rewrite>
but it will also do server/site/otherbsl2/controllertest.aspx
I have tried using ^ to denote the start of the string but it does not work, e.g.
<add input="{PATH_INFO}" pattern="^(bsl2)/(.*)$" />
What am I doing wrong?
Use following regular expression:
/bsl2/(.*)$
or use word boundary \b:
\bbsl2/(.*)$

Is there a Lowercase rewrite rule that will still allow CamelCaps AJAX methods?

On my iis7 box, I have a url rewrite rule in my web.config that keeps all of my urls lowercase:
<rule name="LowerCaseRule1" stopProcessing="false">
<match url="^((?=.*[A-Z]).*\.aspx)(.*)" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{R:1}}{R:2}" />
</rule>
This has been working well, but it also rewrites my AJAX WebMethod calls when they contain any capitals letters. Consequently the methods don't get called. An obvious solution is to keep all WebMethods lowercase, but it would be way more appropriate to attack it on the front from the rewrite's regex.
Currently:
/Default.aspx ==> /default.aspx
/Default.aspx/UpdateOrder ==> /default.aspx/updateorder
I'd like the latter example to rewrite to /default.aspx/UpdateOrder
My regex skills just can't get me there.
Thanks in advance,
John
I set this problem aside and came back to it much later. I added a negate condition and it seems to do the trick:
<rule name="LowerCaseRule1" stopProcessing="false">
<match url="^((?=.*[A-Z]).*\.aspx)(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_URL}" matchType="Pattern" pattern="^([^A-Z]+\.aspx/)(.*)" ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" url="{ToLower:{R:1}}{R:2}" />
</rule>