Apache 웹서버 : RedirectMatch - 페이지 이동
#Apache 웹서버 : RedirectMatch - 페이지 이동
루트 폴더일 경우 다른 폴더 /aa/ 로 이동하고자 할 경우
##1. Apache 설정에서 이동 : 추천
apache 설정 파일에서
**RedirectMatch 사용**
정규표현식을 사용하여 정확히 루트의 파일만 지정할 수 있습니다.
_RedirectMatch permanent : permanent (또는 301) 리다이렉트는 브라우저에 강력하게 캐싱. 테스트 때는 시크릿 모드(Incognito) 창을 열어서 테스트._
_설정이 의도대로 작동하는지 확인하기 전까지는 permanent 대신 temp (또는 302)를 사용하여 테스트하고, 확정 후 permanent로 바꾸는 것이 좋음._
RedirectMatch permanent "^/index.php$" "/aa/"
만일 / /index.php /index.htm /index.html 을 모두 /aa/ 로 이동하고자 할 경우
RedirectMatch permanent "^/$" "/aa/"
RedirectMatch permanent "^/index\.(php|html|htm)$" "/aa/"
최종 : 위의 두 내용을 합친 것과 동일함
**RedirectMatch permanent "^/(index\.(php|html|htm))?$" "/aa/"**
정규 표현식 설명 :
- 요약 : ? = 0 혹은 1회, ^ = 처음 , $ = 끝, () = 그룹
- (...)? : 괄호 안의 내용이 있을 수도 있고 없을 수도 있음을 의미
- ^/index\. : 반드시 도메인 바로 뒤의 /index.으로 시작해야 함 (. 앞에 백슬래시를 붙이는 이유는 정규표현식에서 점이 '모든 문자'를 의미하지 않게 하기 위함)
- (php|html|htm): 괄호 안의 세 단어 중 **하나라도 일치(OR)**하면 매칭.
- $: 문자열이 여기서 끝나야 함을 의미. (즉, /index.php/something 같은 경로는 제외됨)
실제 동작 확인 :
- 요청 주소 (Request) 이동 주소 (Redirect to)
- https://yourdomain.com/ https://yourdomain.com/aa/
- https://yourdomain.com/index.php https://yourdomain.com/aa/
- https://yourdomain.com/index.html https://yourdomain.com/aa/
- https://yourdomain.com/index.php?opt=1 https://yourdomain.com/aa/?opt=1
====
위의 설정을 입력 후 웹 서버 재 시동 (Bash 쉘에서)
**sudo apache2ctl configtest**
**sudo systemctl reload apache2**
---
### ? 이후 옵션이 붙는 경우
RedirectMatch나 Redirect 지시어는 URL에서 **쿼리 스트링(Query String, ? 뒷부분)**을 제외한 경로(Path) 부분만 매칭에 사용합니다. 하지만 리다이렉트가 일어날 때 뒤에 붙은 쿼리 스트링은 기본적으로 유지된 채로 전달됩니다.
요청: https://example.com/index.php?option=bb
결과: https://example.com/aa/?option=bb 로 리다이렉트
만약 쿼리 스트링을 "제거"하고 싶다면?
만약 파라미터가 있든 없든 무조건 /aa/로만 보내고 싶다면 (즉, ?option=bb를 버리고 싶다면), RedirectMatch 대신 **RewriteRule**을 사용해야 합니다.
RewriteEngine On
\# /aa/ 뒤에 ?를 붙여 쿼리 스트링 전송 방지
**RewriteRule ^/(index\.(php|html|htm))?$ /aa/? [R=301,L]**
### 모든 폴더를 그대로 다른 주소로 옮길 경우
RedirectMatch /(.*)$ https://yourdomain.com/$1
---
## 기타 비 추천 방식
###1. Apache 설정에서 RedirectMatch 가 아닌 Redirect 만 사용
Redirect permanent "/index.php" "/aa/"
단점 : 이 경우 루트 폴더외에 모든 하위폴더에서도 영향을 줌.
Redirect는 단순한 접두사(Prefix) 매칭을 수행함.
/index.php/aaa/ , /products/index.php 의 경우에도 위의 조건이 적용될 가능성이 있음.
Redirect permanent "/" "/aa/" : 이 경우 무한 루프에 빠짐 : 에러
###2. index.html 을 작성하여 아래의 코드를 <head> 내부에 삽입
<meta http-equiv="refresh" content="0;URL='/aa/'">
단점 : 속도가 느리고 html 을 조회후 이동하게 되므로 잠시라도 index.html 화면이 보이게 됨
###3. index.php 를 아래과 같이 작성
header("Location: /aa/");
exit();
---
원문 = https://SkyMoon.info/a/HeismeNote/540

하늘