使用 ServerAlias 重寫 Apache URL

使用 ServerAlias 重寫 Apache URL

我有一個運行在 CentOS 7 伺服器上的虛擬主機,它為 2 個 Prestashop 商店提供服務。
在這個虛擬主機設定檔中,我有一個伺服器名稱和一個伺服器別名,每個都指向一個專用儲存。

最近我將這兩個商店都遷移到了 HTTPS,但仍然存在一個問題:我知道如何重寫 URL 以從 HTTP 重定向到 HTTPS,但是我可以根據客戶端請求的 URL 進行重定向嗎?

我知道如何使用 2 個虛擬主機來完成此操作,但由於配置幾乎相同,所以我只想使用一個檔案來完成此操作。

範例:將相同 Vhost conf 檔案中的所有內容重寫http://store1.example.comhttps://store1.example.comAND 。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>

相關內容