Tools: String Manipulations Find Dupes My IP Json Formatter Lorem Ipsum More...
Discussion Options
ajax animation

IIS Rewrite Module to Redirect HTTP to HTTPS on AWS ELB

4/28/2017 11:02:53 AM by: CodeLocker

The following is an IIS Rewrite Module rule for redirecting HTTP request to HTTPS. This is specifically for use with AWS ELB. Since all (usually) requests between the ELB and the EC2 Host are over port 80, you must check the value of the "HTTP_X_FORWARDED_PROTO" header. This shows the protocol used by client.

Some additional comments.

  •  Match Url: robots.txt is the file used by the ELB to check the health of the EC2 host. If this file is being requested, the rule is negated (not applied). It will match all other requests.
  • If the HTTP_X_FORWARDED_PROTO header has a value of https, this rule is negated.
  • If the REMOTE_HOST value is localhost, this value is negated. This is to ensure the rule is disabled during local development.
  • REMOTE_ADDR rules negates the rule for local dev.
  • HTTP_HOST rule negates the rule for local dev.
  • Redirect type is Permanent.
  • {REQUEST_URI} will append everything from the original request.
  • appendQueryString = false to ensure IIS doesn't append the query string ({REQUEST_URI} will take care of the query string.

* The commented out section of the rule contains a hardcoded host name. This may be needed if you are dealing with an IP Leak. This will prevent a private IP from "leaking" and being returned in the response headers.

View Copy: Content Copied
<!--<action type="Redirect" url="https://{HTTP_HOST}/{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />-->


			<rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
				<match url="robots.txt" negate="true" />
					<add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true" />
					<add input="{REMOTE_HOST}" pattern="localhost" negate="true" />
					<add input="{REMOTE_ADDR}" pattern="" negate="true" />
					<add input="{HTTP_HOST}" pattern="localhost" negate="true" />
				<action type="Redirect" url="https://{HTTP_HOST}/{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
				<!--<action type="Redirect" url="{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />-->
Copy: Content Copied
ajax animation

Get Coding

Store your first code or add more code to your collection.
You must login to report abuse. ×