programing

web.config 변환에서 IIS 다시 쓰기 규칙 대체

css3 2023. 9. 7. 21:56

web.config 변환에서 IIS 다시 쓰기 규칙 대체

환경에 따라 달라지기를 원하는 몇 가지 IIS 다시 쓰기 규칙이 있습니다.개발 재작성 규칙은 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>

테스트하기 위해 배포할 때는 앱 설정이 변경되지만 IIS에 의해 다시 쓰기 규칙은 변경되지 않습니다.난 이 모든 것이<rewrite>섹션은 단순히 변환 파일에 있는 섹션으로 대체됩니다(http://msdn.microsoft.com/en-us/library/dd465326.aspx), 에 따르면). 그러나 아무것도 바뀌지 않습니다.

퍼팅을 해봤습니다.xdt:Transform="Replace" xdt:Locator="Match(name)">개별 규칙에 대해서도:

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

하지만 역시 이것은 아무런 차이가 없습니다.

web.config의 rewrite 규칙을 대체할 수도 있습니까? 그렇다면 제가 놓치고 있는 것은 무엇입니까?

메인 web.config에 재작성 규칙이 없었기 때문에 Replace 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>

좋은 점은 예를 든 답이 많지만, 세부적인 내용은 거의 빠진 것 같습니다. 웹사이트에 이것에 대해 적었는데, 여기서 핵심은 다음을 추가하는 것입니다.xdt:Transform="Insert"해당 환경에 대해 추가할 루트 태그 계층 구조.

기본적으로 Web.config 파일이 있지만 Web도 있습니다.debug.config 및 웹.아래 이미지와 같이 Release.config:

enter image description here

응용 프로그램 릴리스에서 http에서 http로 리디렉션을 추가하려고 합니다.그런 다음 웹을 편집합니다.Release.config를 실행하고 다음 행을 추가합니다.

<?xml version="1.0"?>

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

따라서 다음에 프로젝트를 게시할 때 rewrite와 함께 태그가 추가되고 하위 내용이 web.config 파일에 추가됩니다.

게시하기 전에 확인하려면 웹을 마우스 오른쪽 단추로 클릭합니다.Release.config를 클릭하고 미리보기 변환을 클릭합니다.

enter image description here

초기 버전과 릴리스 버전의 차이를 확인할 수 있습니다.

enter image description here

참조:

면책 사항: 본 가이드라인의 링크는 제 개인 웹 사이트를 참조합니다.

처음에 릴리스 구성을 만들 때 재작성 섹션이 이상하게 작동했고 오류와 섹션이 표시되지 않았습니다.이렇게 풀었어요.

Microsoft (R) 빌드 엔진 버전 12.0.31101.0

마이크로소프트.NET Framework 버전 4.0.30319.0

편집 이것을 만지작거리다가 나는 리라이트 플러그인이 없는 서버에서 리라이트 태그를 사용하면 웹 서버가 오류를 반환한다는 것을 깨달았습니다.서버와 로컬 개발 시스템에서 다른 구성을 원하므로 해결 방법은 다음과 같습니다.

변환되지 않은 web.config는 <시스템>만 있으면 됩니다.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>

작업에는 이름이 필요 없지만 다시 쓰기 태그에는 xdt가 필요합니다.변환="삽입"

물론 로컬 컴퓨터에서도 원하는 경우 업데이트가 필요합니다.

시스템의 재작성 부분을 변환할 수 있습니다.웹 서버.저는 처음에 같은 문제를 겪고 있었고 실수로 재작성 노드를 시스템 아래에 잘못 배치했다는 것을 깨달았습니다.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>

내가 사용하는 속임수는 액션에 이름을 붙이는 것입니다.
그럼 내 변신에서 그냥 추가해요.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>

위의 예는 모든 요청에 www를 추가하는 것입니다.

--------UPDATE---------

액션에 이름을 추가하는 업데이트만으로는 원하는 대로 작동하지 않아 코드를 다음과 같이 업데이트했습니다.

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

언급URL : https://stackoverflow.com/questions/11859290/replacing-iis-rewrite-rules-in-web-config-transform