【问题标题】:How can I add an IIS 8 URL rewrite using appcmd?如何使用 appcmd 添加 IIS 8 URL 重写?
【发布时间】:2015-03-05 21:33:58
【问题描述】:

我想在我的服务器上自动创建一些重写规则。不幸的是,执行此操作的所有文档似乎都已过时。 This is the closest I could find on SO 但不幸的是语法不再有效; appcmd 抱怨给定的部分不存在。我已经弄清楚如何解决全局规则集合,但我无法设置任何给定属性。

这是我要插入的 XML 片段:

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

以下是我创建规则元素的方法。这可以正常工作:

appcmd set config -section:globalRules /+"[name='Strip WWW',enabled='true',stopProcessing='true']" /commit:apphost

我现在想创建匹配 URL 元素,并根据上面链接的 SO 问题,我试图猜测语法。但是,这似乎不起作用:

appcmd set config -section:globalRules/rule.[name="Strip WWW"] /match.url:"(.*)" /commit:apphost

显示此错误消息:

错误(消息:未知配置部分“globalRules/rule.[name=Strip WWW]”。替换为?寻求帮助。)

我的猜测是我无法完全指定配置部分 - 除非该错误消息完全不准确。我还尝试了一些其他尝试来猜测该部分的语法:

  • globalRules/rule.[name=Strip WWW]
  • globalRules/rule[name=Strip WWW]
  • globalRules/rule[@name=Strip WWW]

我不确定这个选择方案是什么,但它似乎不是 xpath。如果我能找出它叫什么,我也许能猜出正确的语法。

【问题讨论】:

    标签: iis iis-8 appcmd


    【解决方案1】:

    尝试使用以下语法:

    appcmd set config -section:globalRules /"[name='Strip WWW']".match.url:"(*.)" /commit:apphost

    我发现获得正确语法的最佳方法是在 IIS、配置编辑器中进行更改,然后转到 Generate Script

    【讨论】:

      【解决方案2】:

      我放弃了尝试使用 appcmd 执行此操作,并最终使用 Powershell 执行此操作。我在互联网上所能找到的只是人们在抱怨 appcmd 缺乏这方面的文档;我的猜测是,它已被放弃,取而代之的是更新、更闪亮的 Powershell 模块。

      我找不到任何关于如何执行此操作的可靠文档,因此我编写了自己的脚本来执行此操作。以下是您以相当幂等的方式执行此操作的方法,适用于使用 PS DSC 或厨师或您想使用的任何其他方式进行自动化:

      $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;};
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-04-02
        • 1970-01-01
        • 1970-01-01
        • 2018-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-30
        相关资源
        最近更新 更多