1
votes

This question is probably asked a thousand times, but I still cannot find the solution..

I have a .NET application which, when running locally, redirects from HTTP to HTTPS. But when deployed in an Azure Web App, the redirect won't work.

Based on multiple forum post, my redirect is done with a rule in the Web.config file. Below, you see this file as deployed on azure (I checked with ftp)

  <system.webServer>
    <staticContent>
      <remove fileExtension=".woff" />
      <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
      <remove fileExtension=".woff2" />
      <mimeMap fileExtension=".woff2" mimeType="font/woff2" />
      <remove fileExtension=".json" />
      <mimeMap fileExtension=".json" mimeType="application/json" />
    </staticContent>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="HTTP to HTTPS redirect" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
            <add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
          </conditions>
          <action type="Redirect" url="https://{C:1}/{R:1}" redirectType="Permanent" />
        </rule>
        <rule name="static dist files" stopProcessing="true">
          <match url="^(.+)$" />
          <conditions>
            <add input="{APPL_PHYSICAL_PATH}dist\{R:1}" matchType="IsFile" />
          </conditions>
          <action type="Rewrite" url="/dist/{R:1}" />
        </rule>
        <rule name="index.html as document root" stopProcessing="true">
          <match url="^$" />
          <action type="Rewrite" url="/dist/index.html" />
        </rule>
      </rules>
      <!--<outboundRules>
      <rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
        <match serverVariable="RESPONSE_Strict_Transport_Security"
            pattern=".*" />
        <conditions>
          <add input="{HTTPS}" pattern="on" ignoreCase="true" />
        </conditions>
        <action type="Rewrite" value="max-age=31536000" />
      </rule>
    </outboundRules>-->
    </rewrite>
  </system.webServer>

This Web.config file is the result of a transformation on the original Web.config file, because locally, the port for HTTPS is set to 44362, and therefor the Web.config used locally is a bit different:

    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        <add input="{HTTP_HOST}" pattern="([^/:]*):[^/]*?" />
      </conditions>
      <action type="Redirect" url="https://{C:1}:44362/{R:1}"
          redirectType="Permanent" />
    </rule>

Locally, everythings works fine. Redirect is done in an instant. But when deployed in an azure Web app, redirect is not happening. So "---.azurewebsites.net" won't automattically redirect to "https://---.azurewebsites.net"

I can see though, that he reaches te rule because the title of the browser tab renames for an instant in the name of this rule.

If someone spots the stupid error in this code, please let me know.

Regards,

2

2 Answers

2
votes

There's an extension available in Azure App Service that does this for you automatically.

These directions may change as the Azure Portal changes, but it's current as of 2018-05.

  1. Go to your App Service in the Azure Portal.
  2. Click on "Tools" in the top menu.
  3. In the Develop section, click on "Extensions".
  4. In the Extensions blade, click "Add" at the top.
  5. In the Choose Extensions blade, select "Redirect HTTP to HTTPS".
  6. Click OK or whatever until it's saved.

After that, all of your HTTP traffic will redirect to HTTPS with no other changes required.

0
votes

I've used

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

quite successfully in Azure