On my web.config file, first I need to redirect users requesting URL on http to https. Then, afterwards, I need to do further stuff, I need to make sure www is there in the beginning of the URL. When redirecting users from non-www to www, I want to use the https already set on the previous rule. But I want to do it dinamically, I don't want to hardcode 'https://' on the beginning of my rewrite rule.
The goal is: in the future, if for some reason I need to change this rule from https back to http (I don't think I will, but I may need this), I don't have to go on checking all the redirect rules below to change from 'https' to 'http'. If I use a variable (not sure I should call things inside brackets like {HTTPS} as 'variables'), all I have to do is change the first rule and ignore the rest.
Here is an example so things can get clearer; the key issue I am trying to solve is the item where I have written {WHAT TO USE HERE TO MAKE SURE I USE THE SAME PROTOCOL (HTTP/HTTPS) ALREADY DEFINED ABOVE?}:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<rewrite>
<rules>
<!-- The first rule will do a 301 redirect to https -->
<rule name="Redirect to https" stopProcessing="false">
<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>
<!-- Now, let's make sure we have www if request does not carry it -->
<rule name="Redirect to www" stopProcessing="false">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
</conditions>
<action type="Redirect" url="{WHAT TO USE HERE TO MAKE SURE I USE THE SAME PROTOCOL (HTTP/HTTPS) ALREADY DEFINED ABOVE?}www.mydomain.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</location>
</configuration>
Thank you so much, any help appreciated.