Apache와 Nginx 웹 서버에서 리다이렉션(redirect) 설정하는 방법
Apache 웹 서버에서 리다이렉션 설정
rewrite 모듈 활성화
httpd.conf 편집
vim httpd.confLoadModule rewrite_module modules/mod_rewrite.somod_rewrite.so 모듈 확인
httpd -M | egrep -i rewrite$ httpd -M | egrep -i rewrite
 rewrite_module (shared)http에서 https로 리다이렉트 설정
<VirtualHost *:80>
...
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
...
</VirtualHost>https에서 https로 리다이렉트 설정
<VirtualHost *:443>
...
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ https://scbyun.com$1 [R=301,L]
#RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
...
</VirtualHost>리다이렉션 규칙
# HTTP에서 HTTPS로 리다이렉트
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# 특정 경로에서 다른 경로로 리다이렉트
Redirect /old-page /new-page
# 도메인 리다이렉트
Redirect 301 / http://newdomain.com/Nginx 웹 서버에서 리다이렉션 설정
http에서 https로 리다이렉션 설정
server {
    listen 80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
...
}https에서 https로 리다이렉션 설정
server {
    listen 443 ssl http2;
    server_name sangchul.kr;
    return 301 https://scbyun.com$request_uri;
...
}리다이렉션 규칙
server {
    listen 80;
    server_name yourdomain.com;
    location / {
        return 301 https://$host$request_uri;
    }
}
server {
    listen 443 ssl;
    server_name yourdomain.com;
    # SSL 설정
    ssl_certificate /etc/nginx/ssl/yourdomain.crt;
    ssl_certificate_key /etc/nginx/ssl/yourdomain.key;
    # 나머지 HTTPS 설정
    # ...
    location / {
        # 웹 애플리케이션 또는 리다이렉션 설정
        # ...
    }
}Rewrite 코드 상세분석
| 지시자 | 설명 | 
| RewriteEngine | rewriting engine 활성화 여부 | 
| RewriteCond | RewriteRule과 함께 사용되는 규칙으로 RewriteCond 다음에 오는 RewriteRule은 RewriteCond에서 설정한 패턴과 일치하는 경우만, Rule을 실행한다. | 
| RewriteRule | 실질적인 Rewrite 규칙들을 적용하는 지시자. 사용법? RewriteRule [Input URL] [Return URL] [Input URL]에는 Perl정규식을 사용할 수 있다. | 
Rewrite 지시자 (정규식)
| 지시자 | 설명 | 
| . | 다수의 한 문자 | 
| ? | 0개 이상의 한 문자 | 
| * | 0개 이상의 문자 또는 문자열 | 
| + | 1개 이상의 문자 또는 문자열 | 
| (chars) | (, ) 안의 문자 또는 문자열을 그룹으로 묶는다. 이 문자 그룹은 [Return URL]에서 $N 의 변수로 활용할 수 있다. | 
| ^ | 문자열의 첫 문(열)을 지정 | 
| $ | 문자열의 끝 문자(열)을 지정 | 
| \(역슬래쉬) | 정규표현식에서 특별한 의미로 사용되는 문자의 특수기능을 제거 | 
| {n} | 정확히 n번 반복 | 
| {n,} | n번 이상 반복 | 
| {n,m} | n 이상 m 이하 반복 | 
| [chars] | 문자들의 범위 또는 표현할 수 있는 문자들을 설정합니다. | 
예) [a-z] : a 부터 z 까지의 소문자, [tT] : 소문자 t 또는 대문자 T
Rewrite Subrutine 지시자
| 지시자 | 설명 | 
| L | 뒤구문 여부를 무시하고 그 줄에서 끝낸다. | 
| N | 새로운 Rule이 시작된다는 의미. | 
| R | Redirection. 무조건 넘긴다. 뒤 주소로 넘긴다는 의미. | 
| NC | 대소문자를 구별하지 않는다. | 
| OR | 프로그래밍의 or와 유사. | 
| QSA | Cond의 내용을 지난 결과에 덧붙인다. | 
| NE | Out 될 값에 특수문자가 HexCode로 되어 포함된 경우. | 
예시의 R=301은 moved permanently. 즉, 그 주소로 영구 이동하는 것을 나타낸다.
참고URL
- 변군이글루 블로그 : 웹서버에서 http를 https(SSL)로 리다이렉트하는 방법
- Apache : URL 재작성 지침서
- Nginx Documentation : Converting rewrite rules
- Today I Learned. @cheers_hena 치얼스헤나 : 아파치 HTTP -> HTTPS 리다이렉트 설정하기
'리눅스' 카테고리의 다른 글
| [draft] 우분투에서 UTF-8 언어셋으로 변경하는 방법 (0) | 2025.10.31 | 
|---|---|
| [draft] CentOS 7에서 lsyncd와 rsync를 사용하여 파일의 실시간 동기화를 설정하는 방법 (0) | 2025.10.31 | 
| [draft] 리눅스 환경에서 rsync 명령어로 데이터 동기화하는 방법 (0) | 2025.10.31 | 
| [draft] rsync 스크립트 (0) | 2025.10.31 | 
| [draft] 우분투에 rsync를 설치하고 설정하는 방법 (0) | 2025.10.31 | 
 
                  
                 
                  
                 
                  
                