본문 바로가기
Development/CodeIgniter

코드이그나이터(CodeIgniter)쓸 때 URL에서 index.php 없애기

by wansdream 2011. 9. 22.
CodeIgniter를 기본설정으로 쓰면 URL가 다음과 같이 된다.
http://domain.com/index.php/page1
http://domain.com/index.php/page2

물론 인덱스페이지는 http://domain.com으로도 접속되지만...
하위 메뉴 등으로 넘어가면 중간에 index.php가 들어가야 한다.

URL에서 index.php가 보기 싫을 때,,, 없애는 방법이 있다.

우선 확인사항 부터...
아파치 설정파일 httpd.conf 에서
해당 디렉토리의 설정이 AllowOverride None 으로 되어 있으면 안된다.
AllowOverride All 로 바꿔준다.
설정 파일 변경 후에는 반드시 아파치 재기동!

이제..
index.php가 위치하는 디렉토리에
.htaccess가 없으면 추가해주고 다음과 같이 입력해주자...
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

이제 아래 URL으로도 접속된다 ^^
http://domain.com/page1
http://domain.com/page2

※주의점
URL가 다음과 같을 때는 설정이 조금 달라진다.
http://domain.com/ci/index.php/page1
http://domain.com/ci/index.php/page2

.htaccess 파일을 아래와 같이 설정해야 한다.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php/$1 [L]

그러면 http://domain.com/ci/page1 로 접속가능하다.


조금 응용하여 다음과 같이 변경하여 사용중이다.
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /ci/
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !^/include/(.*)$
  RewriteCond %{REQUEST_URI} !^/img/(.*)$
  RewriteCond $1 !^(index\.php|images|robots\.txt)
  RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
  ErrorDocument 404 /
</IfModule>

주의할 점은 RewriteBase 부분이다.
http://domain.com/ 로 운영 중이라면 RewriteBase /
http://domain.com/ci/ 로 운영 중이라면 RewriteBase /ci/


댓글