2007~2011/IIS2010. 11. 2. 09:50

우리에게 잘 알려진 XpressEngine, Textyle, Textcube 등 많은 웹 어플리케이션들이 Rewrite 동작에 기반한 Fancy URL 을 제공하고 있으며 IIS 에서도 URL Rewrite 모듈을 사용하여 국내 오픈소스 어플리케이션에서 제공하는 Rewrite 규칙을 그대로 이용 가능합니다. 이번 포스팅에서는 URL Rewrite 기능과 관련하여 아래 3가지 규칙을 생성하여 테스트 해 보도록 하겠습니다. 

1.
URL Rewrite 규칙 생성
2. URL Redirect 규칙 생성
3. Access Block 규칙 생성 


URL Rewrite 규칙 생성
첫 번째, URL Rewrite 규칙을 생성하는 방법입니다. 아래 예제를 통해 간단히 URL Rewrite 규칙을 생성하도록 하겠습니다.    


[Fancy URL]

http://localhost/article/555/some-article-title 

[Rewrite URL]
http://localhost/article.php?id=555&title=some-article-title

위와 같이 Fancy URL 은 사람이나 검색 엔진에는 친화적이지만 어플리케이션을 처리하기 위한 요청으로는 적합하지 않기 때문에 아래에 있는 URL 와 같이 URL Rewrite 를 통해 Rewrite 해야 할 필요가 있습니다. 



1. article.php 소스 코드 작성
테스트를 위해 아래와 같이 샘플 코드를 만들고 article.php 파일로 저장하여 웹 사이트 기본 폴더에 복사합니다. http://localhost/article.php 로 접속이 가능하도록 설정되어 있습니다. 


<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>URL Rewrite Module Test</title> 
<body>
<h1>URL Rewrite Module Test Page</h1>
<table>
 <tr>
 <th>Server Variable</th>
 <th>Value</th>
 </tr>
 <tr>
 <td>Original URL: </td>
 <td><?php echo $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL']; ?></td>
 </tr>
 <tr>
 <td>Final URL : </td>
 <td><?php echo $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING']; ?></td>
 </tr>
</table>
<body> 
</html> 




2. URL Rewrite 규칙 생성
 가. IIS 관리자에서 PHP Site 의 기능에서 URL Rewrite 실행 - Add Rule(s) 


 나. Inbound rules - Blank rule 선택 


 다. Name, Pattern, Action 을 정의하고 우측 '작업' 영역에서 '적용' 버튼을 클릭하면 저장됩니다. 


  1) Name : 규칙의 이름 
 
2) Pattern : ^article/([0-9]+)/([_0-9a-z-]+)


   가) 문자열의 시작이 "article/" 로 시작
   나) 하나 이상의 숫자가 "/" 뒤에 나타남
   다) 하나 이상의 "_", "-", 알파벳 문자열이 두 번째 "/" 이후에 존재함

즉, 위와 같은 패턴이 확인되면 아래 Action Rewrite URL 규칙이 동작하게 됩니다.

 3) Action : article.php?id={R:1}&title={R:2}
   가) {R:0} : article/555/some-article-title
   나) {R:1} : 555
   다) {R:2} : some-article-title

즉, R:1 에 Rewrite 된 패턴의 첫 번째 값, R:2 에 두 번째 값이 규칙에 의해 할당됩니다. 


3. 실행 결과
http://localhost/article/555/some-article-title URL 에 접속해 보면 Fancy URL 이 위에서 생성했던 규칙대로 Rewrite 처리 되었음을 확인할 수 있습니다. 



4. URL Rewrite 규칙 저장
URL Rewrite 모듈로 작성한 규칙은 기본 웹 사이트 루트 디렉터리에 포함된 web.config 파일에 자동으로 저장됩니다. Web.config 파일을 열어보면 아래와 같습니다. 

<?xml version="1.0" encoding="UTF-8"?>
<configuration> 
    <system.webServer> 
        <rewrite> 
            <rules> 
                <rule name="Rewrite to article.php"> 
                    <match url="^article/([0-9]+)/([_[0-9a-z-]+)" /> 
                    <action type="Rewrite" url="article.php?id={R:1}&amp;title={R:2}" /> 
                </rule> 
            </rules> 
        </rewrite> 
    </system.webServer>
</configuration> 




URL Redirect 규칙 생성
두 번째, URL Redirect 예제입니다. 

[원본 URL]
http://localhost/blog/some-other-title/777 

[Redirect URL]
http://localhost/article/777/some-other-title 

1. Redirect 규칙 생성
 가. IIS 관리자에서 PHP Site 의 기능에서 URL Rewrite 실행 - Add Rule(s) - Inbound rules - Blank rule 선택
 나. Name, Pattern, Action Type, Redirect URL, Redirect type 


  1) Name : Redirect from Blog
  2) Pattern : ^blog/([_0-9a-z-]+)/([0-9]+)
  3) Action type : Redirect
  4) Redirect URL : article/{R:2}/{R:1} 
  5) Redirect type : Temporary (307) 


2. 실행 결과
인터넷 익스플로러 실행 후 http://localhost/blog/some-other-title/777 URL 을 입력하면 http://localhost/article/777/some-other-title URL 로 Redirect 됨을 확인할 수 있습니다.




Access Block 규칙 생성

세 번째, 요청에 대한 차단 규칙을 만들 수 있는 Access Block 규칙에 대한 예제입니다. URL Path, User-agent Header, IP Address, Query String, Referer, Host Header 를 기반으로 차단 규칙을 생성할 수 있으며 이번 예제에서는 Host Header 에 localhost 와 일치하지 않는 요청에 대해서는 Blocking 하도록 설정하는 방법입니다. 

1. Access Block 규칙 생성
 가. IIS 관리자에서 PHP Site 의 기능에서 URL Rewrite 실행 - Add Rule(s) - Inbound rules - Request blocking 선택    

 나. 요청 차단 규칙 추가
  1) Block access based on : Host Header
  2) Block request that : Does Not Match the Pattern (패턴과 일치하지 않은 요청)
  3) Pattern : localhost
  4) Using : Wildcards
  5) How to block : Abort Request 



2. 실행 결과
 가. Access Block 규칙 적용 전


 나. Access Block 규칙 적용 후



IIS 관리자 또는 Web.config 파일을 통해 3가지 URL Rewrite 규칙을 생성하고 적용하는 방법을 살펴 보았습니다.


작성자 : Lai Go / 작성일자 : 2010.11.02

Posted by Lai Go