Linux/CentOS2012. 9. 13. 11:00

발단은 퍼마링크(permalink:  permanent link, 고유링크) 접속이 불가하여 확인하던 중에 mod_rewrite 설정이 필요했다.

 

1)mod_rewrite이 설치되어 있는지 확인해보자
- 아파치 설정 파일에 다음 라인이 있는지 확인하고 없다면 적고 저장한다
LoadModule rewrite_module modules/mod_rewrite.so
- 아파치 재시작
/etc/init.d/httpd restart
- index.php에 <? phpinfo(); ?> 를 코딩하고 DocumentRoot로 지정된 디렉토리에 저장하자
- 웹브라우저로 index.php 확인하자
apache2handler Loaded Modules 항목에 mod_rewrite이 보이면
이미 rewrite 모듈이 설치된 것이다

 

2)
DocumentRoot로 지정된 디렉토리에(/var/www/html) cloth/freece라는 이름의 하위 디렉토리들을 만들고
# mkdir -p /var/www/html/cloth/freece
그 아래에 123.html 파일을 저장하자
# cd /var/www/html/cloth/freece
# touch 123.html

3)
/var/www/html에 .htaccess 파일을 생성하고 다음을 적어주자
RewriteRuleEngine on
RewriteRule /products/([0-9]+) /products/freece/$1
RewriteRule의 기본 형식은 다음과 같다
RewriteRule Pattern Substitution [Flag(s)]
[0-9]+는 숫자가 하나 이상 있음을 의미한다
예)아래와 같은 형태의 파일이 매칭된다

http://localhost/products/123.html

http://localhost/products/4323.html

http://localhost/products/43374823.html

소괄호로 감싼 부분은 태그를 부여하는 것이다 뒤에서 $1로 참조할 수 있다
 
4)
.htaccess 파일을 사용하기 위해서 /etc/httpd/conf/httpd.conf의 다음 부분을 수정하자
<Directory /var/www/html>
    AllowOverride ALL #원래는 None인데 ALL로 수정하자
</Directory>
설정을 변경했으니 아파치 재시작한다
/etc/init.d/httpd restart

 

5)

테스트를 해볼 차례다
/var/www/html에 test.html을 만들고 다음을 저장하자
<html>
    <head>
        <title>example</title>
    </head>
<body>
<p>
<a href="http://192.168.0.3/products/123.html">test</a>
</p>
</body>
</html>
웹브라우저 주소창에 http://192.168.0.3/test.html을 입력하고 test라는 링크를 클릭하면
http://192.168.0.3/products/123.html 페이지 대신에 http://192.168.0.3/cloth/freece/123.html 페이지가 로드된다

 

6) 
웹서버에 존재하지도 않는 http://192.168.0.3/products/123.html을 주소창에 입력해도
http://192.168.0.3/cloth/freece/123.html로 리다이렉트되기 때문에
오류없이 http://192.168.0.3/cloth/freece/123.html 페이지를 볼 수 있게 된다 

mod_rewrite에 대해서 쉽게 설명한 웹사이트
http://www.workingwith.me.uk/articles/scripting/mod_rewrite
http://www.sitepoint.com/article/guide-url-rewriting/2/

출처: http://blog.naver.com/timberx/30040695435

 

 

 

apache 서버에서 mod_rewrte 가 동작 하도록 설정하기

 

rewrite 모듈은 설치가 되어 있다는 것을 가정한다.

 

httpd.conf 에서

 

DocumentRoot "C:/webroot"
<Directory "C:/webroot">
   Options Includes ExecCGI FollowSymLinks
    AddHandler cgi-script .cgi .pl
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

 

 

AllowOverride  All 이걸 해줘야 웹루트의 .htaccess 파일에 있는 rewrite rule 이 동작한다.

None 로 해두면 무시 된다.

 

Options FollowSymLinks 이 부분도 있어야 동작한다.

 

.htaccess

 

파일에

 

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

 

이렇게 넣어두면

 

http://localhost/users => http://localhost/index.php?url=users 로 맵핑된다.

 

'Linux > CentOS' 카테고리의 다른 글

리눅스 yum 명령어  (0) 2012.10.10
BackupPC for CentOS  (0) 2012.10.09
CentOS Iptables 방화벽 포트 OPEN  (0) 2012.09.07
Wowza Media Server 설치  (0) 2012.09.07
CentoOS DSS(Darwin Streaming Server) 설치  (0) 2012.09.07
Posted by iWithJoy