Back to Top


1397/05/16 - 12:27

Redirect www to non-www and http to https on IIS

When we have https in web, For keeping website integrated, we need redirect all request to single address.

کلمات کلیدی

HttpsHttpredirectnon-wwwwww

Redirect www to non-www and http to https on IIS

1- Redirect Http to Https and www to non-www

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
        </handlers>
        <aspNetCore processPath=".\Landa.Billing.WebApp.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
        <rewrite>
            <rules>
                <rule name="Force non-WWW" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAny">
                        <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
                    </conditions>
                    <action type="Redirect" url="https://{C:2}/{R:1}" appendQueryString="true" />
                </rule>
                <rule name="Force HTTPS" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAny">
                        <add input="{HTTPS}" pattern="off" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

2 - Redirect Http to Https

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
        </handlers>
        <aspNetCore processPath="dotnet" arguments=".\Gamma.SME.WebApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
        <rewrite>
            <rules>
                <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>