Redirect HTTP www and non-www to HTTPS www on IIS Using web.config

0
2712
HTTPS www Redirects on IIS

There are situations when you wish to redirect every request to HTTP with www or non-www version of your website to a URL containing HTTPS and www. This has become important from an SEO perspective because search engines treat HTTP and HTTPS versions of your website as two different websites with exactly the same content. You probably also know that this is a huge problem and your website might even get penalized for this. To overcome this problem, you need to implement 301 redirects on your website.

The process of implementing a 301 redirect depends on the platform you are using. In this article, I am going to show you how to redirect HTTP www and non-www to HTTPS www on IIS using web.config file.

How to Implement HTTPS www Redirects on IIS

Open up the web.config file of your website in your favorite text-editor or IDE, which must be Microsoft Visual Studio if you are working in ASP.NET. The code to implement 301 redirects goes inside the section as shown below:

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="Redirect to WWW" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^domainname.com$" />
                    </conditions>
                    <action type="Redirect" url="https://www.domainname.com/{R:0}" redirectType="Permanent" />
                </rule>
                <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>

Don’t forget to replace domainname.com with your actual domain name. This is a perfectly tested code snippet that I’ve used on tens of websites. If you face any trouble while implementing 301 redirects on your website, let me know in the comments section.

Redirect HTTP www and non-www to HTTPS www on IIS Using web.config
5 (100%) 2 vote[s]

LEAVE A REPLY

Please enter your comment
Please enter your name