준호씨의 블로그

codeigniter index.php 숨기기 본문

개발이야기

codeigniter index.php 숨기기

준호씨 2016. 4. 6. 00:19
반응형
codeigniter 를 처음 사용 할 때 이상한 점은 index.php 가 계속 보인다는 것이다.

예를 들면 다음과 같다.


index.php 를 숨기고 다음과 같이 나타내면 좋을 텐데 말이다.

apache 설정
일단 apache 를 사용 한다면 rewrite_module 이 활성화 되어 있어야 됨. 다른 웹서버를 사용한다면 방법이 좀 달라질 수 있다. 개인적으로 로컬 개발환경에서 php 자체 웹서버를 사용하는 경우 별다른 설정이 필요 없없다.

apache2 기준에서 mods-available 에 rewrite.load 파일 안에 로드 하는 내용이 있음
junho85@junho85:/etc/apache2/mods-available$ cat rewrite.load
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

mods-enabled 에 이미 심볼릭 링크 걸려 있음.
junho85@junho85:/etc/apache2/mods-enabled$ ll rewrite.load
lrwxrwxrwx 1 root root 30 10월 11 17:13 rewrite.load -> ../mods-available/rewrite.load

phpinfo 를 확인 해 봐도 로드 되어 있는지 알 수 있음


로드 되어 있지 않다면 먼저 mod_rewrite 를 로드 시켜 주도록 설정 해 주어야 함.

이렇게 해 주면 .htaccess 를 사용 할 수 있게 됨

CI config.php 설정
config.php 내용 중 다음과 같은 내용이 있는데 여기서 index.php 를 제거 한다.
$config['index_page'] = 'index.php';

아래 처럼 바꾼 다는 말임
$config['index_page'] = '';

.htaccess 설정
ci 가 설치된 디렉토리에 .htaccess 파일을 생성한다. ci 가 설치된 디렉토리는 최초 index.php 가 있는 위치라고 보면 된다.


아래와 같이 작성 하라고 나와 있다.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
의미는 index.php, images, robots.txt 외에는 index.php 를 요청한 것으로 취급한다는 의미라고 한다

여기서 나는 codeigniter 를 /ci 경로로 두고 있어서 마지막 줄을 수정 했다.

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php/$1 [L]

뭔가 또 문제가 생겼다. assets 디렉토리에 있는 js 파일들을 찾지 못했다.

아래 내용을 추가 하면 하위 디렉토리를 찾을 수 있었다. 파일이나 디렉토리가 있으면 index.php 를 거치지 않고 아파치가 직접 처리 하라는 뜻이라고 함.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

인터넷에 떠돌아 다니는 설정을 종합해 보면 대략 다음과 같다. (마지막에 /ci 추가 한 것만 다르다)
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond $1 !^(index\.php|images|captcha|data|include|uploads|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /ci/index.php/$1 [L]
</IfModule>

혹은 RewriteBase 를 바꿔줘도 된다.
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /ci/
    RewriteCond $1 !^(index\.php|images|captcha|data|include|uploads|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>


참고
반응형
Comments