← 전체로 돌아가기
스킬 server

HTTPS 강제 리다이렉트 (.htaccess)

Apache에서 HTTP를 HTTPS로 301 리다이렉트하는 방법

httpshtaccessapacheredirectssl

Apache 서버에서 SSL 설치 후 HTTP 트래픽을 HTTPS로 강제 리다이렉트해야 할 때 mod_rewrite 모듈을 활용하면 돼. .htaccess 파일에 다음 설정을 추가하면 HTTP 요청을 모두 HTTPS로 301 영구 리다이렉트할 수 있어.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  • RewriteEngine On: 리라이트 엔진을 활성화함.
  • RewriteCond %{HTTPS} off: 현재 요청이 HTTPS가 아닐 때 (HTTP일 때) 조건을 만족함.
  • RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]: 위 조건이 맞으면 모든 요청(^(.*)$)을 https://로 시작하는 동일한 URL로 301 영구 리다이렉트함. [L]은 이 규칙이 마지막임을, [R=301]은 301 리다이렉트임을 의미해.

SSL 설치 후 이 설정을 .htaccess 파일에 추가하고 잘 적용됐는지 꼭 확인해야 해.

여기서 배울 것

  1. .htaccess 파일로 HTTPS 강제 리다이렉트 설정하기
  2. `RewriteEngine On`으로 리라이트 모듈 활성화하기
  3. `RewriteCond`로 HTTP 요청 조건 확인하기
  4. `RewriteRule`로 301 영구 리다이렉트 적용하기
원본 파일 보기 (.claude/skills/tn-force-https-htaccess/SKILL.md)
---
name: HTTPS 강제 리다이렉트 (.htaccess)
description: This skill should be used when the user asks to force HTTP traffic to HTTPS using an .htaccess file, or when they need to implement a 301 redirect for HTTPS on an Apache server after SSL installation.
version: 1.0.0
source: /home/son/prj/resume/backup_notes_260317/notion/Tech Note/https , htcaccess b2dcd58aacf24aad9bb53d1a8fe1f468.md
---

# https , htcaccess

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

.htcaccess

after install ssl, check it.