Reputation: 9367
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:
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.
Upvotes: 1
Views: 1831
Reputation: 1926
Try using this syntax:
appcmd set config -section:globalRules /"[name='Strip WWW']".match.url:"(*.)" /commit:apphost
I've found that the best way to get the correct syntax is going in IIS, Configuration Editor, change something and then go to Generate Script
Upvotes: 2
Reputation: 9367
I gave up on trying to do it with appcmd and ended up doing it with Powershell instead. All I could find on the internet was people bemoaning appcmd's lack of documentation for this; my guess is that this has been dropped in favour of the newer, shinier Powershell module.
I couldn't find any reliable documentation on how to do this so I wrote my own script to do this. Here's how you do it in a fairly idempotent style, suitable for automation with PS DSC or chef or whatever else you want to use:
$name = "Strip WWW";
$siteName = "Default Web Site";
$url = "^(.*)$";
$redirectAction = "http://myapp.com{PATH_INFO}";
$hostPattern = "^www\.myapp\.com$";
$sitePath = "IIS:\Sites\$siteName";
$filterXpath = "/system.webserver/rewrite/rules/rule[@name='$name']";
$filter = $(Get-WebConfiguration -PSPath $sitePath -Filter $filterXpath);
if ($filter -eq $null)
{
Add-WebConfigurationProperty -PSpath $sitePath -filter '/system.webserver/rewrite/rules' -name . -value @{name=$name; stopProcessing='True'};
$filter = $(Get-WebConfiguration -PSPath $sitePath -Filter $filterXpath);
}
if ($filter.match.url -ne $url)
{
Set-WebConfigurationProperty -PSPath $sitePath -filter "$filterXpath/match" -name "url" -value $url;
}
if ($filter.action.url -ne $redirectAction)
{
Set-WebConfigurationProperty -PSPath $sitePath -filter "$filterXpath/action" -name . -value @{url=$redirectAction; type="Redirect";};
}
if ($filter.conditions.collection.count -eq 0)
{
Add-WebConfigurationProperty -PSpath $sitePath -filter "$filterXpath/conditions" -name . -value @{input="{HTTP_HOST}"; pattern=$hostPattern};
}
else
{
$condition = $filter.conditions.collection[0];
if ($condition.pattern -ne $hostPattern)
{
Set-WebConfigurationProperty -PSPath $sitePath -filter "$filterXpath/conditions/add[1]" -name "." -value @{pattern=$hostPattern;};
}
}
Upvotes: 0