Apache 서버 .htaccess 파일로 HTTPS 리다이렉트 세팅하기

    Apache 웹 서버를 이용할 때, 사용자가 http://로 접속하더라도 자동으로 보안 연결인 https://로 전환(Redirect)되도록 설정해야 합니다.

    본 가이드에서는 Apache 서버의 설정 파일을 건드리지 않고 개별 웹 루트 디렉토리의 .htaccess 설정 파일만으로 HTTP -> HTTPS 자동 리다이렉트를 구현하는 방법을 안내합니다.

    1. 사전 필수 조건

    • Apache mod_rewrite 모듈이 활성화되어 있어야 함
    • SSL/TLS 인증서가 서버에 정상 설치되어 있어야 함

    2. 단계별 세팅 절차

    [1단계] Apache mod_rewrite 모듈 활성화

    터미널에서 리다이렉트 엔진 모듈을 활성화합니다.

    Bash

    sudo a2enmod rewrite
    sudo systemctl restart apache2
    

    [2단계] Apache AllowOverride All 설정 확인

    .htaccess 파일이 작동하려면 Apache 서버 메인 설정에서 .htaccess 구문을 읽을 수 있도록 허용되어 있어야 합니다.

    /etc/apache2/apache2.conf (Ubuntu) 또는 /etc/httpd/conf/httpd.conf (CentOS) 파일을 점검합니다.

    Apache

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All    # None으로 되어 있다면 All로 변경
        Require all granted
    </Directory>
    

    [3단계] .htaccess 파일 작성

    웹 루트 디렉토리(예: /var/www/html/.htaccess)에 파일을 생성하거나 편집합니다.

    Bash

    nano /var/www/html/.htaccess
    

    파일 최상단에 다음 구문을 삽입합니다.

    Apache

    <IfModule mod_rewrite.c>
    RewriteEngine On
    # HTTPS 접속이 아닌 경우 (off)
    RewriteCond %{HTTPS} off
    # 301 영구 이동을 통해 https 주소로 리다이렉션
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    </IfModule>

    답글 남기기

    이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다