Redirect http to https in IIS using custom errors - Part 103

Suggested Videos
Part 100 - Secure Socket Layer in asp.net
Part 101 - Implementing SSL in asp.net web application
Part 102 - Redirect http to https in IIS

In this video we will discuss about redirecting users from HTTP to HTTPS, using "IIS Error Pages". In the previous session, we discussed about redirecting users using "URL ReWrite" module. 



Custom error pages can be set at the server level or at a specific application level in IIS. In this demo, we will discuss about setting custom error pages at the server level. There are 3 simple steps.

Step 1: 
Make sure "Require SSL" option from "SSL Settings" is checked for your web application in IIS. Now, browse the web site, using HTTP, and you will receive the following error. Pay attention to HTTP error code - 403.4, which we will be using later.
HTTP Error 403.4 - Forbidden
The page you are trying to access is secured with Secure Sockets Layer (SSL).



Step 2:
Copy and paste the following HTML in a notepad and save it as "RedirectToHttps.htm" in "C:\inetpub".
<html>
<head>
   <title>
      Redirecting to HTTPS
   </title>
</head>
<script language="JavaScript">
function redirectHttpToHttps()
{
    var httpURL= window.location.hostname + window.location.pathname;
    var httpsURL= "https://" + httpURL;
    window.location = httpsURL;
}
redirectHttpToHttps();
</script>
<body>
</body>
</html>

Step 3:
1. In IIS, select the "Server Name" and double click "Error Pages" to open the feature.
2. Click on "Add" link under "Actions" 
3. Set Status Code = 403.4, File Path = C:\Inetpub\RedirectToHttps.htm and click "OK"
4. Now click "Edit Feature Settings" link under "Actions"
5. Select "Custom Error Pages" and Path = C:\inetpub\RedirectToHttps.htm

Now, access the application using HTTP. You will be automatically redirected to HTTPS.

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Its Not redirecting to https,
    i have done every thing you told

    ReplyDelete
    Replies
    1. Give network resource access to file "C:\inetpub\RedirectToHttps.htm", then it will start working

      Delete

It would be great if you can help share these free resources