본문 바로가기

Web

http 접속 시 https로 리디렉션 (URL forwarding)

http를 사용하던 사이트에 SSL을 추가하게 되면 http, https 둘 다 접속이 가능한 상태가 된다. 

 

https 가 있는데 http를 쓸 필요는 없으므로, http로 접속하면 https로 리디렉션되도록 설정한다.

(자물쇠 열려있는거 보면 신경이....)

 

아파치 설정파일인 httpd.conf에서도 가능하지만,

 

해당 사이트의 .htaccess에서 수정하면 서버 재구동 없이 적용할 수 있다.

 

아래의 두 줄 추가하면 깔끔!

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

 

 

.htaccess의 전체 코드는 이렇다.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>