[Linux] Apache web server의 rewrite module 사용하기
■ 작동환경
○ OSE : CentOS 7 minimal
○ Runtime Environment : Apache web server 2.4.6, PHP 5.4.16
■ Apache web server 확인
○ Redhat 계열의 linux에서 httpd를 설치하면 rewrite 모듈이 기본적으로 포함되어 있다.
○ Rewrite module 파일이 있는지 확인 : ls /etc/httpd/modules/mod_rewrite.so
○ 로딩되도록 설정되어 있는지 확인 : cat /etc/httpd/conf.modules.d/00-base.conf | grep rewrite
--> 결과 : "LoadModule rewrite_module modules/mod_rewrite.so"
○ Rewrite module 로딩되었는지 확인 : httpd -M | grep rewrite
--> 결과 : "rewrite module (shared)"
■ 사용자별 웹 디렉토리 설정
○ '사용자별 웹 디렉토리 설정파일'에 사용하도록 되어 있는지 확인 : cat /etc/httpd/conf.d/userdir.conf
<Directory "/home/*/public_html"> AllowOverride FileInfo AuthConfig Limit Indexes Options MultiViews SymLinksIfOwnerMatch IncludesNoExec Require method GET POST OPTIONS </Directory> |
- AllowOverride에 "FileInfo"가 있어야 한다.
○ '웹디렉토리 설정 파일'을 다음처럼 설정한다 : vi .htaccess
"RewriteEngine On" : 짧은 주소 엔진을 사용한다.
"RewriteBase /~fact/xe/" : 기본 위치를 사용자별 웹 디렉토리로 잡아준다.
여기위치는 웹브라우저 기준이기 때문에 public_html이 없다.
(웹이 아닌 파일시스템 기준은 ~fact/public_html/xe/ )
■ PHP 확인
○ PHP 모듈 정상 로딩 확인 : http://localhost/phpinfo.php
- phpinfo.php 파일 내용 : <?php phpinfo(); >
--> 결과 : apache2handler 섹션에 있는 표에서 Loaded Modules 항목에 "mod_rewrite"라는 항목이 있으면 된다.
------- 설정 끝 -------
■ Rewrite module 작동 확인 테스트
○ 사용자 웹 디렉토리에 테스트용 PHP 파일을 만들어 정상적으로 rewrite module이 작동하는지 확인한다.
○ 사용자 웹 디렉토리에 접근설정 파일을 아래와 같이 만든다 : vi ~fact/.htaccess
RewriteEngine On RewriteBase /~fact/ RewriteRule ^NotExistFile.htm$ test.php?rewrite=1 |
- 위의 설정은 "bbs.test.com/~fact/NotExistFile.htm" 과 같은 URL로 접속하면
"bbs.test.com/~fact/test.php?rewrite=1" 과 같은 URL로 다시 써서 접속하라는 의미이다.
○ 테스트용 PHP 파일을 만든다 : vi ~fact/test.php
<html> <head> <title>Test rewrite module</title> </head> <body> <a href="NotExistFile.htm">Test rewrite module</a> <br> <br> <?php if($_GET['rewrite']==1) { echo "Result: Using mod_rewrite"; } ?> </body> </html> |
○ 웹브라우저를 띄우고 "bbs.test.com/~fact/test.php"로 접속한다.
- "Test rewrite module"이라는 링크가 나온다. 클릭한다.
- Rewrite module이 작동되지 않으면 오류 페이지가 나오고 작동된다면 "Result: Using mod_rewrite" 라고 나온다.
출처 : https://blog.naver.com/g00dmoney/220158844653