ServerAlias를 사용한 Apache URL 재작성

ServerAlias를 사용한 Apache URL 재작성

저는 2개의 Prestashop 매장을 서비스하는 CentOS 7 서버에서 가상 호스트를 실행하고 있습니다.
이 가상 호스트 conf 파일에는 각각 전용 저장소로 연결되는 ServerName과 ServerAlias가 있습니다.

최근에 두 저장소를 모두 HTTPS로 옮겼지만 한 가지 질문이 남아 있습니다. HTTP에서 HTTPS로 리디렉션하기 위해 URL을 다시 작성하는 방법을 알고 있지만 클라이언트가 요청한 URL을 기반으로 리디렉션할 수 있습니까?

나는 2개의 가상 호스트로 이를 수행하는 방법을 알고 있지만 conf가 거의 동일하므로 하나의 파일로만 수행하고 싶었습니다.

예: 동일한 Vhost conf 파일의 모든 항목 에 http://store1.example.comAND https://store1.example.com를 다시 작성합니다.http://store2.example.comhttps://store2.example.com

답변1

Apache가 설정하는 HTTP_HOST 변수를 사용할 수 있습니다.

<VirtualHost *:80>
  ServerName store1.example.com
  ServerAlias store2.example.com
  RewriteEngine On
  RewriteRule ^/?(.*)$ https://%{HTTP_HOST}/$1 [R=301]
</VirtualHost>

답변2

원하는 대로 하나 이상의 파일에 넣을 수 있지만 가장 간단한 방법은 여러 <VirtualHost>지시문을 사용하는 것입니다.

<VirtualHost *:80>
    ServerName store1.example.com
    Redirect permanent / https://store1.example.com
</VirtualHost>
<VirtualHost *:80>
    ServerName store2.example.com
    Redirect permanent / https://store2.example.com
</VirtualHost>
<VirtualHost *:443>
    ServerName store1.example.com
    ServerAlias store2.example.com
    ...
</VirtualHost>

관련 정보