Wednesday 20 November 2013

Automatically redirect http to https in IIS

Automatically redirect http to https in IIS

IIS doesn’t have a feature to redirect http to https. One way to  achieve this is by modifying IIS custom error page (403.4).

  1. a.
    Create a file called SSLRedirect.asp in your webroot with the following content:

<%
Data = request.servervariables(”QUERY_STRING”)
URL = replace(Data, “403;”, “”)
URL = replace(URL, “http://”, “https://”)
response.Status = “200 OK”
response.redirect URL
%>

Or if you want to redirect it to a single URL, you can change those lines above with this instead

<% Response.Redirect(”https://www.YourDomain.com/”) %>

b.  Or Create a HTML SSLRedirect.html file as shown below :
<html>
<head>
<meta http-equiv="refresh" content="2;url=http://webdesign.about.com">
<title>New Page 1</title>
</head>
<body>
</body>
</html>

  1. Open the IIS MMC tool, select properties for your site, go to the directory security tab, click the edit button at the bottom in secure communications, and then check “Require secure channel (SSL)” option.

  1. In the IIS MMC tool under your site’s properties, go to the custom errors tab, select the 403;4 error type, edit its properties to be a URL in your site and change it to

/SSLRedirect.asp       OR       /SSLRedirect.html



4. Still in the IIS MMC tool, select the properties for the custom error file you have createdpreviously (SSLRedirect.asp), go to the file security tab, click the edit button at the bottom in secure communications, and then UN-check “Require secure channel (SSL)” option.