locked
Redirect to SSL RRS feed

  • Question

  • User-1314346660 posted

    Hello folks,

    I have implemented a redirect to SSL rule in webconfig. It worked fine. The problem I have is that it works so well I cant view in local host arrrrgh!!!

    Can anyone suggest some script that ignores the redirect in local host. I have tried a lot of things but cannot seem to get them to work despite others saying the code is ok.

    This is what I have right now. I am very grateful for any help.

    Billson3000

     <rewrite>
      <rules>
       <rule name="Redirect to https">
        <match url="(.*)"/>
        <conditions>
         <add input="{HTTPS}" pattern="Off"/>
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
       </rule>
      </rules>
    </rewrite>

    Sunday, September 16, 2018 4:45 PM

Answers

  • User283571144 posted

    Hi,

    According to your description, I suggest you could try to use below url rewrite rule to achieve your requirement.

    You could add a new condition to allow the url contains the localhost not redirect to the SSL.

    More details, you could refer to below codes:

        <rewrite>
          <rules>
            <rule name="Redirect to https">
              <match url="(.*)"/>
              <conditions>
                <add input="{HTTPS}" pattern="Off"/>
                <add input="{HTTP_HOST}" negate="true" pattern="localhost" ignoreCase="true" />
              </conditions>
              <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
            </rule>
          </rules>
        </rewrite>

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 17, 2018 5:16 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 17, 2018 7:11 AM

All replies

  • User283571144 posted

    Hi,

    According to your description, I suggest you could try to use below url rewrite rule to achieve your requirement.

    You could add a new condition to allow the url contains the localhost not redirect to the SSL.

    More details, you could refer to below codes:

        <rewrite>
          <rules>
            <rule name="Redirect to https">
              <match url="(.*)"/>
              <conditions>
                <add input="{HTTPS}" pattern="Off"/>
                <add input="{HTTP_HOST}" negate="true" pattern="localhost" ignoreCase="true" />
              </conditions>
              <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
            </rule>
          </rules>
        </rewrite>

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 17, 2018 5:16 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 17, 2018 7:11 AM
  • User-1038772411 posted

    When developing on your local machine, this will also attempt to push you to an SSL version of localhost.

    You can fix this by adding some exceptions using the negate attribute of conditionals.

    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    <system.webServer>
    <rewrite>
    <rules>
    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
    <add input="{HTTP_HOST}" matchType="Pattern" pattern="^localhost(:\d+)?$" negate="true" />
    <add input="{HTTP_HOST}" matchType="Pattern" pattern="^127\.0\.0\.1(:\d+)?$" negate="true" />
    </conditions>
    <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
    </rule>
    </rules>
    </rewrite>
    </system.webServer>
    </configuration>

    The two added conditionals will negate the HTTPS redirect for URLs that contain localhost or 127.0.0.1, including ones where a port is specified, which is important since most development servers listen on a port other than the standard port 80.

    Monday, September 17, 2018 7:36 AM
  • User-1314346660 posted

    Thank you very Brando. You are awesome and a very generous sharer of code. I am very grateful.

    Thank you also MudassarKhan. I am developing in azure web app so don't have the luxury of amending the server settings.

    Awesome.

    Billson3000

    Monday, September 17, 2018 8:23 AM