Skip to content

Instantly share code, notes, and snippets.

@bmatthewshea
Last active April 14, 2020 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmatthewshea/151407753bd8b01ced9d422b7a65587e to your computer and use it in GitHub Desktop.
Save bmatthewshea/151407753bd8b01ced9d422b7a65587e to your computer and use it in GitHub Desktop.
Web.config - IIS - AWS/EC2 REWRITE - Rewrite APEX, NON-WWW & NON-SSL - Catchall
<configuration>
<system.webServer>
...
<rewrite>
<rules>
<!-- To work, DNS subdomains must be wildcarded to this site. -->
<!-- Also, this first rule must be listening for all incoming requests/"default web server". -->
<!-- The first rule runs on anything that doesn't match "www.example.com". -->
<rule name="Catchall redirect to www" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Found" />
</rule>
<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<!-- Uncomment 2nd line for AWS EC2 SSL detection - uncomment 1st line for normal detection: -->
<!-- add input="{HTTPS}" ignoreCase="true" pattern="^OFF$" / -->
<add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
...
</system.webServer>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment