同一伺服器上的兩個網站的 https 重定向錯誤

同一伺服器上的兩個網站的 https 重定向錯誤

我有兩個網站,請稱呼它們為example-1.comexample-2.com部署在同一台伺服器上(CentOS 7.5)兩者均由 apache 在連接埠 80 提供服務。

對於這兩個網站,我(永久)重定向http url 到 https(憑證沒有問題,機器人運作正常)。

我遇到的問題是第二個網站的http版本重定向(永久移動,301)到第一個網站的https(網站的順序根據下面的.conf檔)。

第二個網站重定向到第一個網站時不會發生此重定向(請參閱下面的配置),如果不需要從 http 重定向到 https,一切都會正常工作,即我從https://www.example-2.com

這裡有.conf文件,任何有關如何解決此問題的想法將不勝感激。

Listen 80

<VirtualHost *:80>
    ServerName example-1.com
    Redirect permanent / https://www.example-1.com/
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost *:443>
    DocumentRoot "/var/www/html/example-1"
    ServerName www.example-1.com
    ServerAlias example-1.com

## logging
   ErrorLog "/var/log/httpd/example-1-error_log"
   CustomLog "/var/log/httpd/example-1-access_log" combined

        <Directory "/var/www/html/example-1">
                DirectoryIndex index.html index.php
                Options FollowSymLinks
                AllowOverride All
       </Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example-1.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example-1.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example-1.com/chain.pem
</VirtualHost>
</IfModule>
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/plain text/xml application/javascript
</IfModule>

會議第二個網站的(請注意,除了名稱之外,唯一的區別是,在第二個網站中,我們沒有聽80在頂部)

<VirtualHost *:80>
    ServerName example-2.com
    Redirect permanent / https://www.example-2.com/
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost *:443>
    DocumentRoot "/var/www/html/example-2"
    ServerName www.example-2.com
    ServerAlias example-2.com

## logging
   ErrorLog "/var/log/httpd/example-2-home-error_log"
   CustomLog "/var/log/httpd/example-2-home-access_log" combined

        <Directory "/var/www/html/example-2">
                DirectoryIndex index.html index.php
                Options FollowSymLinks
                AllowOverride All
        </Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example-2.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example-2.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example-2.com/chain.pem
</VirtualHost>
</IfModule>
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/plain text/xml application/javascript
</IfModule>

答案1

我想我終於找到了我的配置中的錯誤所在。我將其發佈在這裡以防萬一有人遇到類似的問題。

我將伺服器名稱更改為 www 等效名稱,並添加了不含 www 的伺服器別名,即指令現在變為

<VirtualHost *:80>
    ServerName www.example-1.com
    ServerAlias example-1.com
    Redirect permanent / https://www.example-1.com/
</VirtualHost>

對第二個網站進行相同的調整。這為我解決了不正確的重定向問題。

相關內容