To augment LazyOne's answer, here is an annotated version of the answer.
<rewrite>
<rules>
<clear />
<rule name="Redirect all requests to https" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action
type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"
redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
Clear all the other rules that might already been defined on this server. Create a new rule, that we will name "Redirect all requests to https". After processing this rule, do not process any more rules! Match all incoming URLs. Then check whether all of these other conditions are true: HTTPS is turned OFF. Well, that's only one condition (but make sure it's true). If it is, send a 301 Permanent redirect back to the client at http://www.foobar.com/whatever?else=the#url-contains
. Don't add the query string at the end of that, because it would duplicate the query string!
This is what the properties, attributes, and some of the values mean.
- clear removes all server rules that we might otherwise inherit.
- rule defines a rule.
- name an arbitrary (though unique) name for the rule.
- stopProcessing whether to forward the request immediately to the IIS request pipeline or first to process additional rules.
- match when to run this rule.
- url a pattern against which to evaluate the URL
- conditions additional conditions about when to run this rule; conditions are processed only if there is first a match.
- logicalGrouping whether all the conditions must be true (
MatchAll
) or any of the conditions must be true (MatchAny
); similar to AND vs OR.
- add adds a condition that must be met.
- input the input that a condition is evaluating; input can be server variables.
- pattern the standard against which to evaluate the input.
- ignoreCase whether capitalization matters or not.
- action what to do if the
match
and its conditions
are all true.
- type can generally be
redirect
(client-side) or rewrite
(server-side).
- url what to produce as a result of this rule; in this case, concatenate
https://
with two server variables.
- redirectType what HTTP redirect to use; this one is a 301 Permanent.
- appendQueryString whether to add the query string at the end of the resultant
url
or not; in this case, we are setting it to false, because the {REQUEST_URI}
already includes it.
The server variables are
{HTTPS}
which is either OFF
or ON
.
{HTTP_HOST}
is www.mysite.com
, and
{REQUEST_URI}
includes the rest of the URI, e.g. /home?key=value
- the browser handles the
#fragment
(see comment from LazyOne).
See also: https://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference