替换web中的IIS重写规则。配置转换

时间:2021-06-23 11:28:40

I have some IIS rewrite rules that I want to vary by environment. The development rewrite rules are in the web.config file, then at the end of the web.test.config file I have:

我有一些要根据环境变化的IIS重写规则。开发重写规则在web中。配置文件,然后在web.test结尾。配置文件有:

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

当我部署到测试时,我的应用程序设置会被转换,但是IIS重写规则不会。我希望整个 部分可以简单地替换为转换文件中的部分(如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上替换重写规则。配置,如果是,我缺少什么?

5 个解决方案

#1


42  

As I didn't have any rewrite rules in my main web.config, the Replace transform didn't work. I successfully used the Insert transform, as below:

因为在我的主网络中没有任何重写规则。配置,替换转换不起作用。我成功地使用了Insert transform,如下所示:

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

#2


7  

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

微软。net框架,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 wan't different configurations on server and local developmen machine so the fix is:

修改之后,我意识到在没有重写插件的服务器上使用重写标记会导致webserver返回错误。我不希望服务器和本地开发人员机器上有不同的配置,所以修复是:

The un-transformed web.config only needs a tag and in the web.config.release for a basic canonical host name rule

取消对web。配置只需要一个标签,在web.config中。发布一个基本的规范主机名规则

<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 aswell, it would need an update instead.

显然,如果您希望在本地机器上也使用它,那么它将需要更新。

#3


3  

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="插入"在根标签层次结构中,你想要添加到相应的环境中。

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。debug。配置和Web.Release。配置如下图所示:

替换web中的IIS重写规则。配置转换

Lets say you want to added a redirection from http to https in your release of the application. Then edit Web.Release.config and add following lines:

假设您希望在应用程序的发布中添加从http到https的重定向。然后编辑Web.Release。配置并添加以下行:

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

因此,下次发布项目时,带有重写的标记及其子内容将被添加到web中。配置文件。

To see that before you publish, right click on Web.Release.config and click Preview Transform.

要在发布之前看到这一点,右键单击Web.Release。配置并单击Preview Transform。

替换web中的IIS重写规则。配置转换

You will see the difference between initial version and release version.

您将看到初始版本和发布版本之间的差异。

替换web中的IIS重写规则。配置转换

Reference:

参考:

Disclaimer: the link of this guideline refer to my personal web site.

免责声明:此指南的链接指向我的个人网站。

#4


1  

It is possible to transform the rewrite section of system.webServer. I was initially having the same problem and realized that I had inadvertently placed the rewrite node incorrectly under 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.

可以转换system.webServer的重写部分。我最初遇到了同样的问题,并意识到我无意中将重写节点错误地放在system.web下。虽然这看起来不像是基于您提供的有限的代码片段的问题,但我仍然怀疑您的问题与转换文件中的节点位置有关。

Here is what my Web.Debug.config looks like (and this version is writing the correct Web.config on a debug build):

这是我的Web.Debug。配置看起来是这样的(这个版本正在编写正确的Web)。在调试构建上的配置):

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

#5


0  

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

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

#1


42  

As I didn't have any rewrite rules in my main web.config, the Replace transform didn't work. I successfully used the Insert transform, as below:

因为在我的主网络中没有任何重写规则。配置,替换转换不起作用。我成功地使用了Insert transform,如下所示:

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

#2


7  

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

微软。net框架,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 wan't different configurations on server and local developmen machine so the fix is:

修改之后,我意识到在没有重写插件的服务器上使用重写标记会导致webserver返回错误。我不希望服务器和本地开发人员机器上有不同的配置,所以修复是:

The un-transformed web.config only needs a tag and in the web.config.release for a basic canonical host name rule

取消对web。配置只需要一个标签,在web.config中。发布一个基本的规范主机名规则

<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 aswell, it would need an update instead.

显然,如果您希望在本地机器上也使用它,那么它将需要更新。

#3


3  

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="插入"在根标签层次结构中,你想要添加到相应的环境中。

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。debug。配置和Web.Release。配置如下图所示:

替换web中的IIS重写规则。配置转换

Lets say you want to added a redirection from http to https in your release of the application. Then edit Web.Release.config and add following lines:

假设您希望在应用程序的发布中添加从http到https的重定向。然后编辑Web.Release。配置并添加以下行:

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

因此,下次发布项目时,带有重写的标记及其子内容将被添加到web中。配置文件。

To see that before you publish, right click on Web.Release.config and click Preview Transform.

要在发布之前看到这一点,右键单击Web.Release。配置并单击Preview Transform。

替换web中的IIS重写规则。配置转换

You will see the difference between initial version and release version.

您将看到初始版本和发布版本之间的差异。

替换web中的IIS重写规则。配置转换

Reference:

参考:

Disclaimer: the link of this guideline refer to my personal web site.

免责声明:此指南的链接指向我的个人网站。

#4


1  

It is possible to transform the rewrite section of system.webServer. I was initially having the same problem and realized that I had inadvertently placed the rewrite node incorrectly under 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.

可以转换system.webServer的重写部分。我最初遇到了同样的问题,并意识到我无意中将重写节点错误地放在system.web下。虽然这看起来不像是基于您提供的有限的代码片段的问题,但我仍然怀疑您的问题与转换文件中的节点位置有关。

Here is what my Web.Debug.config looks like (and this version is writing the correct Web.config on a debug build):

这是我的Web.Debug。配置看起来是这样的(这个版本正在编写正确的Web)。在调试构建上的配置):

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

#5


0  

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

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