ServerAlias を使用した Apache URL 書き換え

ServerAlias を使用した Apache URL 書き換え

CentOS 7 サーバー上で vhost を実行しており、2 つの Prestashop ストアにサービスを提供しています。
この vhost conf ファイルには、それぞれ専用のストアに誘導する ServerName と ServerAlias があります。

最近、両方のストアを HTTPS に移行しましたが、1 つの疑問が残っています。HTTP から HTTPS にリダイレクトするために URL を書き換える方法はわかっていますが、クライアントから要求された URL に基づいてリダイレクトすることはできますか?

2 つの vhost でそれを実行する方法はわかっていますが、conf はほぼ同じになるため、1 つのファイルだけで実行したいと考えていました。

例:同じ Vhost conf ファイル内のすべてをANDhttp://store1.example.comに書き換えます。https://store1.example.comhttp://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

必要に応じて 1 つまたは複数のファイルに配置できますが、最も簡単な方法は複数の<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>

関連情報