본문 바로가기

IT

HTTP의 엑세스를 HTTPS로 리다이렉트 시켰습니다.(Windows Server 2016,IIS 10의 URL Rewrite이용)

설정할 때마다 매번 찾아봤던 내용이라 정리를 해둡니다.

 

IIS의 확장모듈 "URL Rewrite(URL 재작성)"을 이용했습니다.

IIS의 기본 모듈이 아니라서 추가로 설치해야 하는데,

웹 플랫폼 인스톨러를 이용해서 설치하거나 

https://www.microsoft.com/web/downloads/platform.aspx

 

Web Platform Installer : The Official Microsoft IIS Site

HomeDownloadsMicrosoft Supported DownloadsWeb Platform Installer Install this extension or view additional downloads  OverviewThe Microsoft Web Platform Installer - WebPI provides a simplified installation workflow for installing common open source web ap

www.microsoft.com

설치 파일을 직접 다운로드해서 설치할 수 있습니다.

https://www.iis.net/downloads/microsoft/url-rewrite

 

URL Rewrite : The Official Microsoft IIS Site

Install this extension or view additional downloads  OverviewIIS URL Rewrite 2.1 enables Web administrators to create powerful rules to implement URLs that are easier for users to remember and easier for search engines to find. By using rule templates, re

www.iis.net

 

"URL Rewrite(URL 재작성)"은 IIS 7.0 버전 이후로 지원됩니다.

"URL Rewrite(URL 재작성)"을 이용하지 않고도 기본 리다이렉트기능을 이용해서 고정된 URL로 이동시키는 것은 가능합니다.

HTTP의 엑세스를 HTTPS로 이동시키는 것도 웹사이트 2개를 만들어 도메인의 HTTP(80)와 HTTPS(443)를 각각 설정하여  HTTP 사이트에 들어오는 모든 엑세스를 기본 리다이렉트를 이용해 도메인의 HTTPS 사이트로 보내는 방식입니다.

하지만 전체 URI 및 파라미터를 유지한 상태로 이동시킨다던지의 상세 처리를 지원하지 않아 결국은 웹어플리케이션 내에서 처리(개발)했었습니다.

 

기본 리다이렉트기능을 이용: 고정된 URL로 이동시키는 것은 가능

http://sample.com
http://sample.com/sample.asp
http://sample.com/sample.asp?p=1

https://sample.com

 

"URL Rewrite(URL 재작성)" 이용: 각각의 URI와 파라메터를 유지하면서 이동시키는 것 같은 상세 설정도 가능

http://sample.com                                        → https://sample.com    
http://sample.com/sample.asp                   → https://sample.com/sample.asp  
http://sample.com/sample.asp?p=1           → https://sample.com/sample.asp?p=1 

 

"URL Rewrite(URL 재작성)"에 2가지의 룰을 추가해서 HTTP의 엑세스를 HTTPS로 보낼 수 있었습니다.

1. HTTPS redirect : 리다이렉트 처리
2. Remove double slash : https://sample.com// URL끝에 슬래쉬가 두개 들어가는 문제 해결 
Rmove로 이름을 잘못 입력한 상태로 캡쳐를 해버렸습니다... 뭐 중요한건 아니므로..

 

1. HTTPS redirect : 리다이렉트 처리

 

Match URL(대상)
Requested URL Matches the Pattern
Using Regular Expressions
Pattern (.*)
Ignore case Check
대소 구분없이 모든 URL을 대상으로 함
Conditions(상태)
Condition Input {HTTPS}
Check if input string Matches the Pattern
Pattern ^OFF$
Ignore case Check
Server Variables의 HTTPS가 OFF상태인지 확인
Action(처리)
Action type Redirect
Redirect URL https://{HTTP_HOST}/{REQUEST_URI}
Append query string Uncheck
Redirect type Permanent (301)
{HTTP_HOST}/{REQUEST_URI}를 그대로 유지하며 https로 301리다이렉트

 

리다이렉션
301 ( Moved Permanently ) 새로운 URL로 영구적으로 변경되었음을 알림
클라이언트에 새로운 URL을 사용하도록 알림
302 ( Found ) 일시적으로 다른 URL로 전환함을 알림
클라이언트는 기존 URL을 그대로 유지
크롤러가 색인화를 하지 않을 가능성이 있음

 

 

2. Remove double slash : https://sample.com// URL끝에 슬래쉬가 두개 들어가는 문제 해결 

 

Match URL(대상)
Requested URL Matches the Pattern
Using Regular Expressions
Pattern (.*)
Ignore case Check
대소 구분없이 모든 URL을 대상으로 함
Conditions(상태)
Condition Input {UNENCODED_URL}
Check if input string Matches the Pattern
Pattern (.*)//(.*)
Ignore case Check
원본 URL에 /가 두개인지 확인
Action(처리)
Action type Redirect
Redirect URL {C:1}/{C:2}
Append query string Uncheck
Redirect type Permanent (301)
원본 URL의 //(슬래쉬가 두개)를 기준으로 앞{C:1}, 뒤{C:2}로 잘라 /(슬래쉬1개)로 재작성한 URL로 301리다이렉트

 

물론 위의 설정은 web.config 파일을 직접 편집으로도 가능합니다.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <system.webServer>
  <rewrite>
   <rules>
    <rule name="HTTPS redirect" stopProcessing="true">
     <match url="(.*)" />
     <conditions>
      <add input="{HTTPS}" pattern="^OFF$" />
     </conditions>
     <action type="Redirect" url="https://{HTTP_HOST}/{REQUEST_URI}" appendQueryString="false" />
    </rule>
    <rule name="Rmove double slash" stopProcessing="true">
     <match url="(.*)" />
     <conditions>
      <add input="{UNENCODED_URL}" pattern="(.*)//(.*)" />
     </conditions>
     <action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
    </rule>
   </rules>
  </rewrite>
 </system.webServer>
</configuration>

 

정규식(Regular Expressions)과 mod_rewrite의 문법은 매번 고생을 합니다만 다행히도 이번은 간단한 설정이었습니다.

 

반응형