โดยปกติเราสามารถ set ให้ IIS Server (Internet Information Server) (7.x เป็นต้นมา) ในเครื่อง Windows Server ให้สามารถใส่ HTTP Response Header เข้าไปได้ แต่มันก็มีความเป็นไปได้ที่ Browser อาจจะไม่ support HSTS Header (พวก IE เวอร์ชั่นเก่าๆ) ดังนั้นเราสามารถ force ให้ user เข้าเป็น HTTPS ได้ ซึ่งสามารถทำได้หลายทางคือ
ใช้ IIS URL Rewrite Module
1. เราสามารถติดตั้ง SSL URL Rewrite Module ได้ โดย download Module Installer ได้จาก
1 |
https://www.iis.net/downloads/microsoft/url-rewrite |
2. เข้าไปที่ C:\inetpubb/wwwroot/ สร้างไฟล์ web.config ขึ้นมา โดยเขียน rule เป็น
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="HTTP to HTTPS redirect" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{SERVER_PORT_SECURE}" pattern="^1$" /> <add input="{SERVER_PORT_SECURE}" pattern="^0$" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer> </configuration> |
3. ทดสอบเข้าใช้งานเว็บไซด์ ตอนนี้น่าจะถูก redirect ไปยัง HTTPS แล้วครับ
กำหนดในหน้า default ของ asp
เราสามารถกำหนดให้ user redirect โดยใช้ code ของ asp ได้ โดยเขียนเป็น
1 2 3 4 5 6 7 |
<% If Request.ServerVariables("HTTPS") = "off" Then Response.Redirect "https://" & Request.ServerVariables("HTTP_HOST") & "/" ElseIf Request.ServerVariables("HTTPS") = "on" Then Response.Redirect "https://" & Request.ServerVariables("HTTP_HOST") & "/" End If %> |
หรือใช้เป็น
1 2 3 4 5 6 7 |
<% If Request.ServerVariables("SERVER_PORT")=80 Then Response.Redirect "https://" & Request.ServerVariables("HTTP_HOST") & "/" ElseIf Request.ServerVariables("SERVER_PORT")=443 Then Response.Redirect "https://" & Request.ServerVariables("HTTP_HOST") & "/" End If %> |
เท่านี้เมื่อ user เข้ามาก็จะถูก redirect ไปยัง https โดยอัตโนมัติแล้วครับ