301 HTML HTTP to HTTPS Redirect on Window server

As now google started giving more seo score to site running on secure URL and you have your SSL on the website. But it is difficult to update all HTML file for the redirect. Here is one solution to this problem.

Use the following code in your web.config

<?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>
    </system.webServer>
</configuration>

You need to have URL Rewrite module. Here is link for download https://www.iis.net/downloads/microsoft/url-rewrite

After change you can check the redirect here https://www.redirect-checker.org/

Test Cases

http://example.com
http://www.example.com

Leave a Reply

Your email address will not be published. Required fields are marked *