IIS - Configuration file is not well-formed XML - regex

<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>

Related

Rewrite rule in web.config Umbraco

I like to use Umbraco with Vue.js and leave all the routing to Vue so I can make SPA. My goal is to write an IIS rewrite rule in web.config for using one Umbraco template for all routes except /umbraco. So far I have this
<rewrite>
<rules>
<rule name="Startview Templating">
<match url="^(http(s)?:\/\/)?(www\.)?([A-Za-z0-9:._-]*)(\/(?!umbraco)([A-Za-z0-9:_-]*))?" />
<action type="Rewrite" url="{R:0}?alttemplate=test" appendQueryString="true"/>
</rule>
</rules>
</rewrite>
This renders the same template (test)on every route I have so that works. But when I enter /umbraco I get a blank page. What am I missing?
Thanks
You should be able to add conditions to the rule to exclude paths, for example:
<conditions>
<add input="{REQUEST_URI}" pattern="^/umbraco/" negate="true" />
</conditions>
In your case i would look something like (I think you can simplify the match url though):
<rewrite>
<rules>
<rule name="Startview Templating">
<match url="^(http(s)?:\/\/)?(www\.)?([A-Za-z0-9:._-]*)(\/(?!umbraco)([A-Za-z0-9:_-]*))?" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/umbraco/" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}?alttemplate=test" appendQueryString="true"/>
</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>

URL Rewrite 2.0 Wildcards - IIS 7.5

<rule name="WomensSilverBangles" patternSyntax="ExactMatch">
<match url="/Bangles/Silver/Womens.aspx" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="Collections/jewellery.aspx?AXSW_CategoryID=Bangles&AXSWFinenessId=Silver&AXSW_GenderID=Womens&MenuName=Jewellery&SiteMapNode=Silver Bangles&depth=2" />
</rule>
Can this be written using wildcards?
As you can see, the three variables in the match URL are used in the rewritten URL string.
I know it's been a while since this question was posted, but there is an excellent article on rewriting here and based on that, you could try experimenting with something like this(not tested):
<rule name="3levelcatchall" patternSyntax="Wildcard">
<match url="/*/*/*.aspx"/>
<action type="Rewrite" url="Collections/jewellery.aspx?AXSW_CategoryID={R:1}&AXSWFinenessId={R:2}&AXSW_GenderID={R:3}&MenuName=Jewellery&SiteMapNode={R:2} {R:1}&depth=2" appendQueryString="false"/>
</rule>