【问题标题】:IIS url rewrite Rules With domain change and httpsIIS url 重写规则与域更改和 https
【发布时间】:2020-10-22 08:05:04
【问题描述】:

我使用下面的代码进行 http 到 https 的重定向,但不知道如何使用多个规则同时更新域和 https。

感谢您的帮助

<!--Examples-->
    Example 1:
    
    input          : http://prodServer/MyApplication        
    Expected Output: https://prodServer.mycompany.com/MyApplication
    
    Example 2:

    input            : https://prodServer/MyApplication        
    Expected Output: https://prodServer.mycompany.com/MyApplication
    
    Example 3   
        
    input            : http://prodServer.mycompany.com/MyApplication        
    Expected Output  : https://prodServer.mycompany.com/MyApplication

代码

 <system.webServer>
            <rewrite>
              <rules>
                <rule name="httpsRedirect" stopProcessing="true">
                  <match url="(.*)" />
                  <conditions>
                    <add input="{HTTPS}" pattern="^OFF$" />
                  </conditions>
                  <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />
                </rule>
              </rules>
            </rewrite>
 </system.webServer>

【问题讨论】:

    标签: asp.net url-rewriting web-config iis-10 url-rewrite-module


    【解决方案1】:

    我们只需为需求编写两个 URL 规则。一个用于将 HTTP 请求强制为 https 请求,另一个用于将主机名转换为实际的服务器名称。

    <rewrite>
                <rules>
        <rule name="Force Https" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{https}" pattern="off" />
                        </conditions>
                        <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />
                    </rule>
                    <rule name="Myrule2" enabled="true" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="prodServer" />
                        </conditions>
                        <action type="Redirect" url="https://prodServer.mycompany.com/MyApplication" />
                    </rule>
                </rules>
            </rewrite>
    

    注意:StopProcessing 标志对 Redirect 动作没有影响,因此当收到 Http 请求时,这两个规则将在服务器端正确执行。
    如果问题仍然存在,请随时告诉我。

    【讨论】:

    • 网址已更改,但此更改后网站无法正常工作,错误消息是“此页面无法正常工作,重定向您太​​多次”。我尝试使用示例 1 链接作为问题。谢谢
    • 我怀疑 URL 规则是否正确应用。请清除您的本地缓存并在私人浏览器窗口中确认。 URL 规则在我这边可以正常工作,此外,我建议您单独测试每个规则。
    【解决方案2】:

    它对我有用

    <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
        <add input="{HTTP_HOST}" pattern="^prodServer$" />
     </conditions>
    

    而不是

    <conditions>
         <add input="{HTTPS}" pattern="^OFF$" />
         <add input="{HTTP_HOST}" pattern="prodServer" />
     </conditions>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-17
      • 2014-10-27
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      • 2013-08-14
      相关资源
      最近更新 更多