回答

4

下面是一個適用於我的示例,您必須使用XPath選擇器才能通過xdt:Transform屬性將您的規則注入適當的位置。

<?xml version="1.0" encoding="utf-8"?> 
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 
    <system.webServer> 
     <rewrite> 
      <rules> 
       <rule name="CanonicalHostNameRule1" enabled="true" stopProcessing="true" 
        xdt:Transform="InsertBefore(/configuration/system.webServer/rewrite/rules/rule[position() = 1])" 
       > 
        <match url="(.*)" /> 
        <conditions> 
         <add input="{HTTP_HOST}" negate="true" pattern="^www\.yoursite\.com$" /> 
        </conditions> 
        <action type="Redirect" url="http://www.yoursite.com/{R:1}" /> 
       </rule> 
      </rules> 
     </rewrite> 
    </system.webServer> 
</configuration>

您還可以使用xpath語句做一些其他有趣的替換。我希望上面的例子很有幫助,因爲現在這種類型的事情我通常首先看到StackOverflow。

+0

請標記爲答案,非常有幫助! –