4

I am trying to setup URL Rewrite on my IIS so that it handles http and let's encrypt. My goal is the following

1) All traffic to http://example.com, http://www.example.com and https://www.example.com should redirect (301) to https://example.com

2) Any subpage and querystring should be kept so that http://www.example.com/whatever/login.aspx?username=blabla would become https://example.com/whatever/login.aspx?username=blabla

3) All requests to http://example.com/.well-known/acme-challenge/* and http://www.example.com/.well-known/acme-challenge/* (where "*" can be whatever subpage and querystring) should NOT be redirected

It is like I have tried everthing but I cannot make it work.

Anders
  • 567
  • 1
  • 7
  • 23

1 Answers1

9

You can do that with two rules. The first one will redirect to https, the second one will change the domain. You need to add your URL .well-known/acme-challenge/ as a condition with "negate" attribute

    <rule name="CanonicalHostNameRule">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAll">>
            <add input="{HTTP_HOST}" pattern="^www.example\.com$" />
            <add input="{REQUEST_URI}" pattern="^/.well-known/acme-challenge" negate="true" />
        </conditions>
        <action type="Redirect" url="https://example.com/{R:1}" />
    </rule>
    <rule name="Redirect to https">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAll">
            <add input="{HTTPS}" pattern="off" />
            <add input="{REQUEST_URI}" pattern="^/.well-known/acme-challenge" negate="true" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
    </rule>
WildJoe
  • 5,740
  • 3
  • 26
  • 30
Victor Leontyev
  • 8,488
  • 2
  • 16
  • 36