Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Rewrite http to https and let's encrypt

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.

like image 818
Anders Avatar asked Jan 29 '23 04:01

Anders


1 Answers

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>
like image 56
Victor Leontyev Avatar answered Feb 12 '23 18:02

Victor Leontyev