Search code examples
asp.neturl-rewritingweb-configiis-10url-rewrite-module

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>

Solution

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