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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With