0
votes

I'm trying to add a redirection rule in web.config to only serve my webpage in HTTPS and no HTTP at all; so far I've used this ruleset (together with my 404 redirect):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
       <rewrite>
            <rules>
                <clear />
                <rule name="Redirect to https" stopProcessing="true">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTPS}"  pattern="off" ignoreCase="true" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
                </rule>
            </rules>
        </rewrite>
        <directoryBrowse enabled="false" />
        <httpErrors errorMode="Custom" existingResponse="Auto">
            <remove statusCode="404" subStatusCode="-1" />
            <error statusCode="404" subStatusCode="-1" prefixLanguageFilePath="" path="index.php" responseMode="Redirect" />
        </httpErrors>
    </system.webServer>
</configuration>

However, this leads to ERR_TOO_MANY_REDIRECTS; what is wrong with this web.config to cause this? It seems to work for other users.

I have already tried suggestions in these posts; Redirect loop when forcing HTTPS, URL Rewrite causing redirect loop, this blog and this blog.

1

1 Answers

2
votes

Maybe I came here a bit late, but the following Rewrite rule helps to solve the redirection error:

<rule name="Redirect to https" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
    <add input="{HTTP_HOST}" pattern="^example\.com$" />
    <add input="{SERVER_PORT}" pattern="^80$" />
  </conditions>
  <action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>