Apache-2.2

在 Apache 中使用 SSL 將一個虛擬主機重定向到另一個虛擬主機

  • April 12, 2016

這是我的情況:我有兩個指向同一個 IP/機器的 CNAME 域(domain.one.com和),我想將兩者之一重定向到domain.two.com另一個,比如. 有一個有效的 SSL 證書,如果重要的話。domain.one.com/anything``domain.two.com/anything``domain.two.com

我有以下設置:

httpd.conf

RewriteCond %{HTTP_HOST}   ^domain\.two\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         https://domain.two.com/$1 [L,R]

NameVirtualHost *:80
NameVirtualHost domain.two.com:443

<VirtualHost *:80>
   ServerName domain.two.com
   DocumentRoot "/var/www/html/"
   ErrorLog logs/error_log
   CustomLog logs/access_log common
   RewriteEngine On
   RewriteOptions Inherit
</VirtualHost>

ssl.conf

<VirtualHost domain.two.com:443>
   DocumentRoot "/var/www/html"
   ServerName domain.two.com:443
</VirtualHost>

到目前為止我已經嘗試過:

添加到httpd.conf以下內容:

NameVirtualHost domain.one.com:443

<VirtualHost domain.one.com:80>
   ServerName domain.one.com
   RewriteEngine On
   RewriteCond %{HTTP_HOST} ^domain.one.com$
   RewriteRule (.*) http://domain.two.com/$1 [R=Permanent]
</VirtualHost>
  • 不重定向。

還將以下內容添加到ssl.conf

<VirtualHost domain.one.com:443>
   ServerName domain.one.com
   RewriteEngine On
   RewriteCond %{HTTP_HOST} ^domain.one.com$
   RewriteRule (.*) http://domain.two.com/$1 [R=Permanent]
</VirtualHost>
  • 伺服器停止工作。

我知道我做錯了什麼,但我不知道是什麼。我怎樣才能使這項工作?

(我應該提到我在這些事情上是個菜鳥。)

我建議您在 DNS 設置中使用 A 記錄而不是 CNAME 以簡化程序。參考A、CNAME、ALIAS 和 URL 記錄之間的差異

以下 vhost 配置將滿足您的要求。

  1. HTTP domain.one.com 到HTTPS domain.two.com
NameVirtualHost *:80
NameVirtualHost *:443

<VirtualHost *:80>

   ServerName domain.one.com
   RewriteEngine On
   RewriteRule ^(.*)$       "https://domain.two.com$1" [R,L]

   ErrorLog logs/error_log
   CustomLog logs/access_log common

   DocumentRoot "/var/www/html/"

   <Directory "/var/www/html/">
       Require all granted
       Options Indexes FollowSymLinks
       AllowOverride All
       Order allow,deny
       Allow from all
   </Directory>

</VirtualHost>

<VirtualHost *:443>

   ServerName domain.two.com
   SSLEngine On
   SSLCertificateFile /etc/apache2/ssl/your.crt
   SSLCertificateKeyFile /etc/apache2/ssl/your.key
   SSLCertificateChainFile /etc/apache2/ssl/chain.crt

   ErrorLog logs/error_log
   CustomLog logs/access_log common

   DocumentRoot "/var/www/html/"

   <Directory "/var/www/html/">
       Require all granted
       Options Indexes FollowSymLinks
       AllowOverride All
       Order allow,deny
       Allow from all
   </Directory>

</VirtualHost>
  1. 對於HTTPHTTPS domain.one.com 到HTTPS domain.two.com
NameVirtualHost *:80
NameVirtualHost *:443

<VirtualHost *:80>

   ServerName domain.one.com
   RewriteEngine On
   RewriteRule ^(.*)$       "https://domain.two.com$1" [R,L]

   ErrorLog logs/error_log
   CustomLog logs/access_log common

   DocumentRoot "/var/www/html/"

   <Directory "/var/www/html/">
       Require all granted
       Options Indexes FollowSymLinks
       AllowOverride All
       Order allow,deny
       Allow from all
   </Directory>

</VirtualHost>

<VirtualHost *:443>

   ServerName domain.one.com
   RewriteEngine On
   RewriteRule ^(.*)$       "https://domain.two.com$1" [R,L]

   SSLEngine On
   SSLCertificateFile /etc/apache2/ssl/your.crt
   SSLCertificateKeyFile /etc/apache2/ssl/your.key
   SSLCertificateChainFile /etc/apache2/ssl/chain.crt

   ErrorLog logs/error_log
   CustomLog logs/access_log common

   DocumentRoot "/var/www/html/"

   <Directory "/var/www/html/">
       Require all granted
       Options Indexes FollowSymLinks
       AllowOverride All
       Order allow,deny
       Allow from all
   </Directory>

</VirtualHost>


<VirtualHost *:443>

   ServerName domain.two.com
   SSLEngine On
   SSLCertificateFile /etc/apache2/ssl/your.crt
   SSLCertificateKeyFile /etc/apache2/ssl/your.key
   SSLCertificateChainFile /etc/apache2/ssl/chain.crt

   ErrorLog logs/error_log
   CustomLog logs/access_log common

   DocumentRoot "/var/www/html/"

   <Directory "/var/www/html/">
       Require all granted
       Options Indexes FollowSymLinks
       AllowOverride All
       Order allow,deny
       Allow from all
   </Directory>

</VirtualHost>

引用自:https://serverfault.com/questions/769724