Hướng dẫn chuyển từ HTTP sang HTTPS trong IIS

Method 1: Using IIS URL Rewrite Module

For this you will have to install the URL Rewrite module. (FYI, this is available for IIS 7 and higher only.)

Download from herehttp://www.iis.net/downloads/microsoft/url-rewrite

Once installed, the URL Rewrite module would be listed under IIS section. There are few articles out there on this. Here are few to list:

  1. http://www.sslshopper.com/iis7-redirect-http-to-https.html
  2. http://www.jppinto.com/2010/03/automatically-redirect-http-requests-to-https-on-iis7-using-url-rewrite-2-0/

These articles are definitely a great repository, however I observed that they have not addressed an important factor.

As specified in the above links add the below section in the web.config at the root of the site:

  <?xml version="1.0" encoding="UTF-8"?>
  <configuration>
  <system.webServer>
  <rewrite>
  <rules>
  <rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAny">
  <add input="{SERVER_PORT_SECURE}" pattern="^0$" />
  </conditions>
  <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
  </rule>
  </rules>
  </rewrite>
  </system.webServer>
  </configuration>

In the above rule I'm checking whether the server variable "SERVER_PORT_SECURE" is set to 1 or 0. (I'm doing a permanent redirect in the above URL, it can be changed accordingly as per the requirement)

If you want to include the query string in the re-written url, then you can add appendQueryString="true" under the action section.

You can find the complete list of IIS Server variables here: http://msdn.microsoft.com/en-us/library/ms524602(v=vs.90).aspx

SERVER_PORT_SECURE A string that contains either 0 or 1. If the request is being handled on the secure port, then this is 1. Otherwise, it is 0.

Alternatively, instead of the above server variable the following server variable "HTTPS" and "SERVER_PORT" can also be used correspondingly.

NOTE: Ensure the rewrite rule is disabled at each of the virtual directories/applications under the Default Web Site. Due to inheritance, the rule will cause the requests to end up in infinite loop calling itself repeatedly.

Method 2: Using IIS Default Document (a default.asp page)

In this method we will introduce a sample asp page at the root of the website and then add the following piece of code:

  <%
  If Request.ServerVariables("HTTPS") = "off" Then
  Response.Redirect "https://" & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("UNENCODED_URL")
  ElseIf Request.ServerVariables("HTTPS") = "on" Then
  Response.Redirect "https://" & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("UNENCODED_URL")
  End If
  %>

Alternatively you could use the port numbers in the above code to achieve the same (ensure to change the port numbers as per the website configuration).

Method 3: Using IIS HTTP Redirect Module

This is one of the simplest methods, but has a lot of limitations and ideally not used. Here is how we do it:

PRE-REQUISITES: HTTP Redirect module is installed and the website has a valid HTTPS binding in place.

  • Launch the IIS Manager.
  • Go to the HTTP Redirect module.
  • Fill the details as per the requirement as shown below:

This may not be ideal for all the scenarios as the user is redirected to a specified URL.

  • 1 Người dùng thấy hướng dẫn này hữu ích
Hướng dẫn này có hữu ích?

Những hướng dẫn liên quan

Cài đặt SSL trên Mdaemon

Bước 1: Copy 2 files cert được gửi cho bạn và tải lên server (gồm 2 files certificate.crt và...

Cài đặt SSL trên Cpanel

Đăng nhập vào tài khoản cPanel của bạn Xác định vị trí và bấm vào SSL/TLS Manager trong phần...

Cài đặt SSL trên Microsoft Exchange 2010

Để cài đặt chứng thư số SSL cho Exchange Server 2010, bạn thực hiện như sau :  Bước 1: Copy 2...

Cài đặt SSL trên Microsoft Office Communications Server (OCS) 2007

Để cài đặt chứng thư số SSL cho Microsoft Office Communications Server (OCS) 2007, bạn thực hiện...

Cài đặt SSL trên DirectAdmin

Thao tác 3 bước để cài đặt SSL cho hosting sử dụng DirectAdmin: 1) Mở quyền sử dụng SSL cho...