简体   繁体   中英

IIS url rewrite Rules With domain change and https

I used below code for http to https redirect, but don't know how to update domain and https at same time with multiple rules.

Thanks for your help

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

code

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

We just write two URL rules for the requirement. One for forcing the HTTP request to https request, another for converting the hostname to the actual server name.

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

Note: StopProcessing flag has no influent on the Redirect action, thereby these two rules will be performed properly on the server-side when an Http request received.
Feel free to let me know if the problem still exists.

it worked for me with

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

instead of

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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