# IIS redirect HTTP to HTTPS with web.config: URL Rewrite rules and HSTS

> Copy-paste web.config rules to redirect HTTP to HTTPS on IIS with the URL Rewrite module, keep Let's Encrypt validation working, pick www or non-www, and add HSTS safely, with IIS 10's built-in HSTS as an alternative.

Source: https://zopanel.net/windows/blog/iis-redirect-http-to-https  
Updated: 2026-11-27

## Key takeaways

- The standard way to redirect HTTP to HTTPS on IIS is a URL Rewrite rule in web.config that matches {HTTPS} off and redirects with 301.
- Exclude /.well-known/acme-challenge/ so Let's Encrypt renewals keep working.
- Do not use the HTTP Redirect feature with "Require SSL" for this; it causes loops or 403 errors.
- Add HSTS only after HTTPS works for every name, and start with a short max-age.

To redirect HTTP to HTTPS on IIS, install the URL Rewrite module and add a rule to the site's `web.config` that matches requests where `{HTTPS}` is `off` and answers with a permanent (301) redirect to `https://{HTTP_HOST}/{R:1}`. The site needs both an HTTP binding on port 80 and an HTTPS binding on 443 for this to work. Below is the rule, a version that keeps Let's Encrypt validation working, a www/non-www variant, and how to add HSTS.

## Before you start

- The **URL Rewrite** module must be installed. It is a free download from Microsoft, not a Windows feature. Without it, a `<rewrite>` section makes the whole site return **500.19**.
- The site must have a working HTTPS binding and certificate. If it does not yet, see [free SSL certificates on IIS with Let's Encrypt](/windows/blog/free-ssl-certificate-iis-lets-encrypt).
- Keep the port 80 binding. The redirect has to receive the HTTP request before it can send the visitor to HTTPS.

Check both bindings:

```powershell
Import-Module WebAdministration
Get-WebBinding -Name 'example.com' | Select-Object protocol, bindingInformation, sslFlags
```

## The basic HTTP to HTTPS rule

Put this inside `<configuration>` in the site's root `web.config`, or merge the `<rule>` into an existing `<rules>` list:

```xml
<system.webServer>
  <rewrite>
    <rules>
      <rule name="HTTP to HTTPS" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
          <add input="{HTTPS}" pattern="^OFF$" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>
```

How it works:

- `match url="(.*)"` captures the path (without the leading slash and without the query string) as `{R:1}`.
- The condition is true only for plain HTTP requests. Patterns are case-insensitive by default, so `^OFF$` matches `off`.
- The Redirect action appends the original query string by default (`appendQueryString="true"`), so `?page=2` survives.
- `stopProcessing="true"` ends rule processing; put this rule first so other rules do not run on HTTP requests.

`redirectType="Permanent"` sends a 301. While you test, use `Found` (302): browsers cache 301 redirects hard, and a mistake is then stuck in your own browser.

## Keep Let's Encrypt renewals working

HTTP-01 validation fetches `http://<name>/.well-known/acme-challenge/<token>`. Let's Encrypt follows redirects, but if the HTTPS side is broken (for example, the certificate has just expired), validation fails and you cannot fix it by renewing. Exclude the challenge path:

```xml
<rule name="HTTP to HTTPS" stopProcessing="true">
  <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}" redirectType="Permanent" />
</rule>
```

## Choose www or non-www in the same step

Redirecting twice (HTTP to HTTPS, then www to non-www) costs an extra round trip. One rule can do both. This one sends `www.example.com` on either scheme to `https://example.com`:

```xml
<rule name="Canonical host" stopProcessing="true">
  <match url="(.*)" />
  <conditions>
    <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}" redirectType="Permanent" />
</rule>
```

Put it before the HTTP to HTTPS rule. The certificate must cover `www.example.com` too, or visitors who type `https://www.` see a certificate warning before the redirect happens.

## Behind a load balancer or CDN

If TLS ends at a load balancer or CDN that talks to IIS over HTTP, `{HTTPS}` is always `off` and the rule loops. Test the forwarded header instead:

```xml
<add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" />
```

Only trust that header if IIS can only be reached through the proxy; otherwise anyone can send it.

## What not to do

- **"Require SSL" plus custom 403 redirects.** The old trick of ticking **Require SSL** and turning the 403.4 error into a redirect is fragile and breaks ACME validation. Use URL Rewrite.
- **The HTTP Redirect feature on the whole site.** It redirects HTTPS requests too, and with the same site serving both, it loops.
- **Redirecting to `{SERVER_NAME}`** if the site has several host names. Use `{HTTP_HOST}` so each name keeps its own host.

## Add HSTS

HTTP Strict Transport Security tells browsers to use HTTPS for your domain for a set time, even if someone types `http://`. Turn it on only after HTTPS works for every host name, because browsers will refuse plain HTTP for the whole `max-age`.

**Option 1: IIS 10 built-in HSTS.** IIS 10 version 1709 and later, which includes Windows Server 2019, 2022 and 2025, has an `hsts` element per site that can also do the HTTP to HTTPS redirect:

```powershell
Import-Module IISAdministration
Start-IISCommitDelay
$sites = Get-IISConfigSection -SectionPath 'system.applicationHost/sites' | Get-IISConfigCollection
$site  = Get-IISConfigCollectionElement -ConfigCollection $sites -ConfigAttribute @{ name = 'example.com' }
$hsts  = Get-IISConfigElement -ConfigElement $site -ChildElementName 'hsts'
Set-IISConfigAttributeValue -ConfigElement $hsts -AttributeName 'enabled' -AttributeValue $true
Set-IISConfigAttributeValue -ConfigElement $hsts -AttributeName 'max-age' -AttributeValue 300
Set-IISConfigAttributeValue -ConfigElement $hsts -AttributeName 'redirectHttpToHttps' -AttributeValue $true
Stop-IISCommitDelay
```

This lives in `applicationHost.config`, not in `web.config`, and its redirect has no exception for the ACME challenge path.

**Option 2: an outbound rule in web.config.** This adds the header only to HTTPS responses, as the standard requires:

```xml
<rewrite>
  <outboundRules>
    <rule name="Add HSTS">
      <match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
      <conditions>
        <add input="{HTTPS}" pattern="on" />
      </conditions>
      <action type="Rewrite" value="max-age=300" />
    </rule>
  </outboundRules>
</rewrite>
```

Start with `max-age=300` (five minutes), check every page and subdomain, then raise it to `31536000` (one year). Add `includeSubDomains` only if every subdomain has HTTPS, and `preload` only if you understand that removal from browser preload lists takes a long time.

## FAQ

### Why does my IIS HTTPS redirect cause "too many redirects"?

Usually TLS ends at a proxy and `{HTTPS}` is always off, or the HTTP Redirect feature is also turned on. Check with `curl -I http://example.com/` and follow each `Location` header.

### Should I use 301 or 302?

301 for the final setup; 302 while testing.

### Does the rule keep the query string?

Yes. The Redirect action appends it by default.

### Do I still need port 80 open after redirecting?

Yes, for the redirect itself and for Let's Encrypt HTTP-01 renewals.

## In Zopanel for Windows

In Zopanel, the site's **Domain and HTTPS** tab has an **Always use HTTPS** switch. It writes a 301 URL Rewrite rule on IIS that leaves `/.well-known/acme-challenge/` alone, so renewals keep working; this is available on Server 2019, 2022 and 2025. Path and whole-site redirects and the preferred name (with or without `www`) are limited. Zopanel does not add an HSTS header to websites for you today; add it with one of the options above. See [Websites and IIS](/windows/docs/websites) and [SSL certificates](/windows/docs/ssl). Free plan: 1 server, 10 sites. [Download](/windows/download) or see [pricing](/windows/pricing).
