0

We have tried many solution for our website to redirect non www to www version and http to https version. But its not working. Sharing you some of the code snippets we have tried.

<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>

And

<system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
      <httpErrors>
        <remove statusCode="404" subStatusCode="-1" />
        <remove statusCode="403" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="/404/page.html" responseMode="ExecuteURL" />
        <error statusCode="403" prefixLanguageFilePath="" path="/404/page.html" responseMode="ExecuteURL" />
      </httpErrors>
      <rewrite>
        <rules>
          <rule name="Redirect to WWW" stopProcessing="true">
            <match url=".*" />
            <conditions>
              <add input="{HTTP_HOST}" pattern="^creative-sols.com$" />
              <add input="{URL}" pattern="\.axd$" negate="true" />
            </conditions>
            <action type="Redirect" url="http://www.creative-sols.com/{R:0}" redirectType="Permanent" />
          </rule>
          <rule name="RewriteASPX">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAll">
              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
              <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
              <add input="{URL}" pattern="\.axd$" negate="true" />
            </conditions>
            <action type="Rewrite" url="{R:1}.aspx" />
          </rule>
        </rules>
      </rewrite>
      <httpProtocol>
        <customHeaders>
          <clear />
        </customHeaders>
      </httpProtocol>
    </system.webServer>

One more solution we have tried.. IIS Redirect non-www to www AND http to https

There is solution from Microsoft community also

<rewrite>
    <rules>
        <rule name="HTTP Redirect to HTTPS" enabled="true" stopProcessing="true">
            <match url="(.*)" ignoreCase="false" />
            <conditions>
                <add input="{HTTPS}" pattern="off" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
        </rule>
    </rules>
</rewrite>



<rewrite>
    <rules>
        <rule name="Redirects to www.creative-sols.com" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTP_HOST}" pattern="^example.com$" />
            </conditions>
            <action type="Redirect" url="https://www.example.com/{R:0}" />
        </rule>
    </rules>
</rewrite>

but all the solutions are failed. Please note that our website is hosted on vps windows server and the domain is creative-sols.com. Looking forward for the help!

  • 1) Use privacy tabs of browsers. 2) Enable FRT to learn more, https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules – Lex Li Feb 01 '23 at 16:55
  • Did you try the solution specified here? https://blog.elmah.io/web-config-redirects-with-rewrite-rules-https-www-and-more/ – ThomasArdal Feb 02 '23 at 09:24

1 Answers1

0

You can also try below rules:

<rule name="https fixer" stopProcessing="true">
  <match url=".*$" />
    <conditions logicalGrouping="MatchAll">
       <add input="{HTTPS}" pattern="^OFF$" />
       <add input="{HTTP_HOST}" pattern="^creative-sols.com$" negate="true" />                   
    </conditions>              
  <action type="Redirect" url="https://www.creative-sols.com{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>   
        
<rule name="www fixer" stopProcessing="true">
  <match url=".*$" />
    <conditions logicalGrouping="MatchAll">
      <add input="{HTTP_HOST}" pattern="www" negate="true" />
      <add input="{HTTP_HOST}" pattern=" creative-sols.com" negate="true" />
    </conditions>
  <action type="Redirect" url="https://creative-sols.com{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>
samwu
  • 3,857
  • 3
  • 11
  • 25