Search code examples
asp.netxmliishttpsweb-config

How to redirect url to ssl url on localhost in web.confg


localhost:5011 is url and localhost:44330 is SSL url. I want to redirect http://localhost:5011 to https://localhost:44330 in my web.config. I found rule for this but it is not worked as I wanted:

<rule name="Redirect local requests to https" stopProcessing="true">
    <match url="(localhost:5011*)" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
    </conditions>
    <action type="Redirect" url="https://localhost:44330/" redirectType="Permanent" appendQueryString="true" />
</rule>   

This redirects to https://localhost:5011. How can I fix this?


Solution

  • I think your rul won't redirect Because<match url= ? doesn't contain hostname and port number.

    If you need to redirect http://localhost:5011 to https://localhost:44330, please try this rule

     <rule name="Redirect local requests to https" stopProcessing="true">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAll">
                            <add input="{HTTPS}" pattern="off" />
                            <add input="{HTTP_HOST}" pattern="localhost" />
                            <add input="{SERVER_PORT}" pattern="^5011$" />
        </conditions>
        <action type="Redirect" url="https://localhost:44330/" redirectType="Permanent" appendQueryString="true" />
    </rule>   
    

    IIs shouldn't redirect http://localhost:5011 to https://localhost:5011, so please check whether you have other rewrite rule or your application is doing the same work.