Search code examples
http-redirectiishttpsweb-configchain

Redirect chain problem in web.config, Windows Server 2020 IIS


Lets say I have https://helloworld.com as my website. What is the best practice now to fill up the web.config file? My SEO analyzer complaining about redirect chain problem, that https is redirected to http. But why? What did I do wrong? I have the following generic web.config file on my server that had been working for a long time, but apparently not anymore:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <defaultDocument>
            <files>
                <remove value="index.php" />
                <remove value="default.aspx" />
                <remove value="iisstart.htm" />
                <remove value="index.html" />
                <remove value="index.htm" />
                <remove value="Default.asp" />
                <remove value="Default.htm" />
                <add value="start.asp" />
            </files>
        </defaultDocument>
        <httpProtocol>
         <customHeaders>
           <remove name="X-Powered-By" />
          </customHeaders>
        </httpProtocol>
        <rewrite>
            <rules>
                <rule name="CanonicalHostNameRule1">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^helloworld\.com$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://helloworld.com/{R:1}" />
                </rule>
                <rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAny">
                        <add input="{SERVER_PORT_SECURE}" pattern="^0$" />
                    </conditions>
                 <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

I googled the problem but could not find the right syntaxt to resolve it.


Solution

  • For the rule HTTP/S to HTTPS Redirect, change the <match url="(.*)" /> to <match url="(^helloworld\.com$)" />. you don't need a catch-all redirect expression, just one that handles a certain domain name.