0

I have to redirect a full URL from HTTP to HTTPS for an Angular app that needs to be hosted on an IIS server.

I created the following rules, but it is not working, it only works with the main domain (like http://www.somedomain.com, it redirects just fine, but but with http://www.somedomain.com/route

Here are my rules. What am I doing wrong?

<rules>
    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
    </rule>
    <rule name="Angular Routes" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="./index.html" />
    </rule>
</rules>

The idea is to redirect http://www.somedomain.com/route/something or http://somedomain.com/route/something to https://www.somedomain.com/route/something

Uwe Keim
  • 2,370
  • 4
  • 29
  • 46
Pablo Varela
  • 103
  • 1
  • 3

2 Answers2

0

you are doing it right, but IIS has its wired cache so sometimes you dont see changes right away, as far as I remember it somehow can survive iisreset. Also, Google Chrome spotted to remember redirects.

So suggestion is to wait a little or reboot both machines (sounds crazy I know)

Here is my working config. Same idea but different condition.

     <rules>
        <rule name="http to https" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{SERVER_PORT}" pattern="443" negate="true" />
            <add input="{HTTP_HOST}" pattern="\.local$" negate="true" />
            <add input="{URL}" pattern="/robots.txt" negate="true" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
      </rules>
  • "IIS has its wired cache", probably you are referring to your browser's cache. Always use private mode of your browser for such testing, and you will discover a brand new world. – Lex Li Dec 17 '20 at 17:31
0

There are several ways to do the same redirection. If you are using url="https://{HTTP_HOST}/{R:1}" you have to add appendQueryString="true", otherwise URI path will not be appended.

So the config should look like:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

But I'm normally using another rule with 301 redirect which is better handled by browsers at some conditions. In such case you have to use another type of uri: url="https://{HTTP_HOST}{REQUEST_URI}"

<configuration>
 <system.webServer>
    <rewrite>
     <rules>
       <rule name="HTTPS force" enabled="true" stopProcessing="true">
       <match url="(.*)" />
       <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
       </conditions>
       <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
     </rule>
    </rules>
   </rewrite>
 </system.webServer>
</configuration>
Hardoman
  • 225
  • 1
  • 7