简体   繁体   English

在 web.config 转换中替换 IIS 重写规则

[英]Replacing IIS rewrite rules in web.config transform

I have some IIS rewrite rules that I want to vary by environment.我有一些 IIS 重写规则,我希望这些规则因环境而异。 The development rewrite rules are in the web.config file, then at the end of the web.test.config file I have:开发重写规则在 web.config 文件中,然后在 web.test.config 文件的末尾我有:

    <appSettings>
         ...Some app settings tranforms here
    </appSettings>
    <system.webserver>
            <rewrite xdt:Transform="Replace">
              <rules>
                ... rules here
              </rules>
            </rewrite>
          </system.webserver>
        </configuration>

My app settings are getting transformed when I deploy to test, but by IIS rewrite rules are not.当我部署到测试时,我的应用程序设置正在转换,但 IIS 重写规则没有。 I was hoping the entire <rewrite> section would simply be replaced with the one in the transform file (as per http://msdn.microsoft.com/en-us/library/dd465326.aspx ), but nothing is changing.我希望整个<rewrite>部分可以简单地替换为转换文件中的部分(根据http://msdn.microsoft.com/en-us/library/dd465326.aspx ),但没有任何变化。

I have tried putting xdt:Transform="Replace" xdt:Locator="Match(name)"> on the individual rules too:我也尝试将xdt:Transform="Replace" xdt:Locator="Match(name)">放在各个规则上:

<rule name="Test rule" stopProcessing="true" xdt:Transform="Replace" xdt:Locator="Match(name)">

But again this makes no difference.但这同样没有区别。

Is it even possible to replace rewrite rules in the web.config and if so, what am I missing?甚至可以替换 web.config 中的重写规则,如果可以,我错过了什么?

As I didn't have any rewrite rules in my main web.config, the Replace transform didn't work.由于我的主 web.config 中没有任何重写规则,因此替换转换不起作用。 I successfully used the Insert transform, as below:我成功地使用了插入转换,如下所示:

  <system.webServer>
<rewrite xdt:Transform="Insert">
  <rules>
    <rule name="CanonicalHostNameRule1">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www\.mysite\.com$" negate="true" />
      </conditions>
      <action type="Redirect" url="http://www.mysite.com/{R:1}" />
    </rule>
  </rules>
</rewrite>
</system.webServer>

There is a lot of answers here with examples which is a good thing, but I think few details are missing.这里有很多带有示例的答案,这是一件好事,但我认为缺少一些细节。 I have wrote about this in my website , the key point here is to add xdt:Transform="Insert" in the root tag hierarchy you want to be added for the respective environment.我已经在我的网站上写过这个,这里的关键点是在要为相应环境添加的根标记层次结构中添加xdt:Transform="Insert"

By default you have your Web.config file, but you have also Web.Debug.config and Web.Release.config as seen in the image below:默认情况下,您有 Web.config 文件,但也有 Web.Debug.config 和 Web.Release.config,如下图所示:

在此处输入图片说明

Lets say you want to added a redirection from http to https in your release of the application.假设您想在应用程序版本中添加从 http 到 https 的重定向。 Then edit Web.Release.config and add following lines:然后编辑 Web.Release.config 并添加以下行:

<?xml version="1.0"?>

.....
  <system.webServer>
    <rewrite xdt:Transform="Insert">
      <rules>
        ......
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

So next time you publish your project the tag with rewrite and its sub-content will be added to web.config file.因此,下次您发布项目时,带有 rewrite 的标记及其子内容将添加到 web.config 文件中。

To see that before you publish, right click on Web.Release.config and click Preview Transform.要在发布之前查看,请右键单击 Web.Release.config,然后单击预览转换。

在此处输入图片说明

You will see the difference between initial version and release version.您将看到初始版本和发布版本之间的区别。

在此处输入图片说明

Reference:参考:

Disclaimer: the link of this guideline refer to my personal web site.免责声明:本指南的链接是指我的个人网站。

The rewrite section worked weirdly to me at first when creating the release configs, errors and sections not showing at all.起初,在创建发布配置时,重写部分对我来说很奇怪,错误和部分根本没有显示。 This is how i solved it.这就是我解决它的方法。

Microsoft (R) Build Engine version 12.0.31101.0 Microsoft (R) 构建引擎版本 12.0.31101.0

Microsoft .NET Framework, version 4.0.30319.0 Microsoft .NET Framework,版本 4.0.30319.0

Edit After messing about with this i realized that having the rewrite tag on a server that does not have the rewrite plugin make the webserver return an error.编辑在搞乱这个之后,我意识到在没有重写插件的服务器上使用重写标签会使网络服务器返回错误。 I want different configurations on server and local development machine so the fix is:我想要服务器和本地开发机器上的不同配置,所以修复是:

The un-transformed web.config only needs a <system.webServer> tag and in the web.config.release for a basic canonical host name rule未转换的 web.config 只需要一个 <system.webServer> 标记,并且在 web.config.release 中用于基本规范主机名规则

<configuration>
<system.webServer>
        <rewrite xdt:Transform="Insert">
            <rules>
                <rule name="CanonicalHostNameRule" xdt:Transform="Insert">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^www\.host\.com$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://www.host.com/{R:1}" />
                </rule>
            </rules>
        </rewrite>
</system.webServer>
</configuration>

The action didn't need a name at all but the rewrite tag needs the xdt:Transform="Insert"该操作根本不需要名称,但重写标签需要 xdt:Transform="Insert"

Obviously if you want it on your local machine as well, it would need an update instead.显然,如果您也希望在本地计算机上使用它,则需要进行更新。

It is possible to transform the rewrite section of system.webServer.可以转换 system.webServer 的重写部分。 I was initially having the same problem and realized that I had inadvertently placed the rewrite node incorrectly under system.web.我最初遇到了同样的问题,并意识到我无意中将重写节点错误地放置在 system.web 下。 While this does not look like your problem based on the limited snippet that you provided, I would still suspect that your issue is related to node placement in the transform file.虽然根据您提供的有限片段,这看起来不像您的问题,但我仍然怀疑您的问题与转换文件中的节点放置有关。

Here is what my Web.Debug.config looks like (and this version is writing the correct Web.config on a debug build):这是我的 Web.Debug.config 的样子(这个版本在调试版本上编写了正确的 Web.config):

<?xml version="1.0"?>

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!--
    In the example below, the "SetAttributes" transform will change the value of 
    "connectionString" to use "ReleaseSQLServer" only when the "Match" locator 
    finds an atrribute "name" that has a value of "MyDB".

    <connectionStrings>
      <add name="MyDB" 
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>
  -->
  <system.web>
    <!--
      In the example below, the "Replace" transform will replace the entire 
      <customErrors> section of your web.config file.
      Note that because there is only one customErrors section under the 
      <system.web> node, there is no need to use the "xdt:Locator" attribute.

      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      </customErrors>
    -->
  </system.web>
  <system.webServer>
    <rewrite xdt:Transform="Replace">
      <rules>
        <clear/>
        <rule name="Canonical Hostname">
          <!-- Note that I have stripped out the actual content of my rules for the purposes of posting here... -->
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

A trick I use is give the action a name我使用的一个技巧是给动作一个名字
then in my transform just add xdt:Transform="SetAttributes" xdt:Locator="Match(name)" like the following然后在我的转换中添加xdt:Transform="SetAttributes" xdt:Locator="Match(name)"如下所示

<system.webServer>
<rewrite>
  <rules>

    <rule name="RedirecttoWWW" enabled="true"  >
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
      </conditions>
      <action name="AddWWW" type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
    </rule>

  </rules>
</rewrite>

The above example is to add www to all requests上面的例子是在所有请求中添加www

-------UPDATE----- - - - -更新 - - -

just an update adding name to the action will not work as wanted so I updated the code as the following只是向操作添加名称的更新将无法正常工作,因此我将代码更新如下

 <system.webServer>

      <rule name="RedirecttoWWW" enabled="true"  xdt:Transform="RemoveAll" xdt:Locator="Match(name)" >
      </rule>
      <rule name="RedirecttoWWW" enabled="true"  xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)" >
        <match url="(.*)" />
        <conditions>
          <add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
        </conditions>
        <action  type="Redirect" url="http://{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
      </rule>
    </rules>
  </rewrite>
  </system.webServer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM