I would like to automate the creation of some rewrite rules on my servers. Unfortunately it seems that all documentation to do this is out of date. This is the closest I could find on SO but unfortunately the syntax is no longer valid; appcmd complains about the given section not existing. I have figured out how to address the global rules collection but I am unable to set any of the given properties.
Here is the XML fragment I would like to insert:
<system.webServer>
<rewrite>
<globalRules>
<rule name="Strip WWW" enabled="true" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.myapp\.com$" />
</conditions>
<serverVariables>
</serverVariables>
<action type="Redirect" url="http://myapp.com{PATH_INFO}" />
</rule>
</globalRules>
</rewrite>
</system.webServer>
Here's how I create the rule element. This works correctly:
appcmd set config -section:globalRules /+"[name='Strip WWW',enabled='true',stopProcessing='true']" /commit:apphost
I now want to create the Match URL element, and according to the above linked SO question I tried to guess the syntax. However, this doesn't seem to work:
appcmd set config -section:globalRules/rule.[name="Strip WWW"] /match.url:"(.*)" /commit:apphost
This error message is shown:
ERROR ( message:Unknown config section "globalRules/rule.[name=Strip WWW]". Replace with ? for help. )
My guess is that I'm just not able to specify the configuration section completely - unless that error message is totally inaccurate that is. I have also tried some other attempts at guessing the syntax for the section:
- globalRules/rule.[name=Strip WWW]
- globalRules/rule[name=Strip WWW]
- globalRules/rule[@name=Strip WWW]
I'm not sure what this selection scheme is but it doesn't seem to be xpath. If I could find out what it was called I might be able to guess the correct syntax.