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 Utilizadores acharam útil
Esta resposta foi útil?

Artigos Relacionados

Cài đặt SSL trên IIS 5.x/6.x

Để cài đặt chứng thư số SSL cho IIS 6 trên Windows 2003, bạn thực hiện như sau :  Bước 1:...

Cài đặt SSL trên IIS 7/7.5 (Windows Server 2008)

Để cài đặt chứng thư số SSL cho IIS 7/7.5 trên Windows 2008, bạn thực hiện như sau :  Bước 1:...

Cài đặt SSL trên IIS 8/8.5 (Window Server 2012)

Để cài đặt chứng thư số SSL cho IIS 8/8.5 trên Windows Server 2012, bạn thực hiện như sau :...

Cài đặt SSL trên Apache Linux

Để cài đặt chứng thư số SSL lên Apache trên Linux, bạn thực hiện như sau: 1. Giải nén file đính...

Cài đặt SSL trên Apache Windows

Để cài đặt chứng thư số SSL cho Apache trên Windows, bạn thực hiện như sau: 1. Giải nén file...