简体   繁体   English

Web.config IIS URL重写

[英]Web.config iis url rewrite

I am trying to add a iis url rewrite rule that differs in the development environment (on my local machine) and once uploaded to the server and published (deployed). 我试图添加一个iis url重写规则,该规则在开发环境中(在我的本地计算机上)不同,并且一旦上载到服务器并发布(部署)。

My IIS Rewrite Rule works, but I don't want to have to remember to change localhost to my server hosting address once I deploy the website. 我的IIS重写规则有效,但是我不想记住在部署网站后将localhost更改为服务器托管地址。 Any suggestions? 有什么建议么?

Below are my IIS URL Rewrite rules in <system.webServer> 以下是<system.webServer>中我的IIS URL重写规则

<!-- IIS Rules Rewrite -->
<rewrite>
  <rules>  
<!-- Serve site map with proper XML content type response header. -->
    <rule name="Sitemap XML" enabled="true" stopProcessing="true">
      <match url="sitemap.xml" />
      <action type="Rewrite" url="sitemap.aspx" appendQueryString="false"/>
    </rule>
    <!-- Access block rule - is used to block all requests made to a Web site if those requests do not have the host header set. This type of rule is useful when you want to prevent hacking attempts that are made by issuing HTTP requests against the IP address of the server instead of using the host name -->
    <rule name="Fail bad requests">
      <match url=".*"/>
      <conditions>
        <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
      </conditions>
      <action type="AbortRequest" />
    </rule>
    <!-- HTTP to HTTPS Rule 
    <rule name="Redirect to https" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" negate="false" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
    </rule>-->

I think you can use configuration file transformation (msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx) 我认为您可以使用configuration file transformation (msdn.microsoft.com/zh-cn/library/dd465318(v=vs.100).aspx)

NOTE: The whole system.webServer section will be replaced with whatever put inside the web.release.config 注意:整个system.webServer部分将被替换为web.release.config

web.config web.config

<?xml version="1.0"?>
<configuration>
    <system.webServer>
        <rewrite>
          <rules>
            <rule name="Sitemap XML" enabled="true" stopProcessing="true">
              <match url="sitemap.xml" />
              <action type="Rewrite" url="sitemap.aspx" appendQueryString="false"/>
            </rule>
            <rule name="Fail bad requests">
              <match url=".*"/>
              <conditions>
                <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
              </conditions>
              <action type="AbortRequest" />
            </rule>
          </rules>
        </rewrite>
    </system.webServer>
</configuration>

web.release.config web.release.config

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.webServer xdt:Transform="Replace">
        <rewrite>
          <rules>
            <rule name="Sitemap XML" enabled="true" stopProcessing="true">
              <match url="sitemap.xml" />
              <action type="Rewrite" url="sitemap.aspx" appendQueryString="false"/>
            </rule>
            <rule name="Fail bad requests">
              <match url=".*"/>
              <conditions>
                <add input="{HTTP_HOST}" pattern="YOUR_NEW_SERVER_URL_HERE" negate="true" />
              </conditions>
              <action type="AbortRequest" />
            </rule>
          </rules>
        </rewrite>
    </system.webServer>
</configuration>

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

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