0
votes

I'm struggling to get the web.config file working for redirects - I'm doing this for the first time. I've been trying plenty of different codes which I found on this site and elsewhere. We have HTTPS site and I need to redirect HTTP url-s to HTTPS. The website is written in HTML and we have Windows hosting.

I set up a web.config file and added the code below.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <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>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Am I missing something here? Or does it need some time to take effect?

Many thanks in advance!

Kadri

1
Okay, so what is happening? What is / are there any errors?Can O' Spam
Nothing happens. The http doesn't get redirected to https. No error messages.Kadri

1 Answers

0
votes

From Scott Hanselman's blog a few years ago, you should try this:

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