0

I've these two rules in my web config file to force https and remove www from url.

 <rule name="Remove WWW" stopProcessing="true">
      <match url="^(.*)$" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^(https?://)?www\.(.+)$" />
      </conditions>
      <action type="Redirect" url="{C:1}{C:2}" redirectType="Permanent"  appendQueryString="false"/>
    </rule>



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

Note: I've gone through almost all the questions here on SO and almost all the blogs but no luck yet :(. Some of the SO posts are as;

Proper method to remove www from address using IIS URL Rewrite

URL Rewrite Remove WWW From HTTPS

http://madskristensen.net/post/url-rewrite-and-the-www-subdomain

http://www.serverintellect.com/support/iis/url-rewrite-to-redirect-www-iis7/

http://www.bradymoritz.com/iis7-url-rewrite-remove-www-from-all-urls/

....

Community
  • 1
  • 1
c-sharp
  • 573
  • 1
  • 9
  • 25

1 Answers1

0

Make sure you are putting your rules in the web.config under system.webServer - > rewrite -> rules.

This should do the job for you:

<system.webServer>
 <rewrite>
  <rule name="Redirect to https" stopProcessing="true">
     <match url="(.*)" />
     <conditions>
        <add input="{HTTPS}" pattern="Off" />
     </conditions>
     <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
  </rule>
  <rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
     <match url="*" />
     <conditions>
        <add input="{CACHE_URL}" pattern="*://www.*" />
     </conditions>
     <action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
  </rule>
 <rewrite>
</system.webServer>
Andy-Delosdos
  • 3,560
  • 1
  • 12
  • 25