web-dev-qa-db-ja.com

example.comからexample.netへのファントム仮想ホストの転送

Ubuntu 16.04 LTSでApache2を実行しています。SSL証明書用にCertbotを暗号化します。

2つのドメインがあります。 httpまたはhttpswwwまたはnon-wwwに関係なく、それぞれhttps://example.comhttps://example.netに着陸したい。

ドメインごとに4つの.confファイルがあります:HTTP WWWHTTP NON-WWWHTTPS WWWHTTPS NON-WWWHTTPS NON-WWWは常に最終結果でなければなりません。

サイト01:example.com

http://example.com -forwards to- https://example.com [OK]
http://www.example.com -forwards to- https://example.com [OK]
https://www.example.com -forwards to- https://example.com [OK]
https://example.com works as expected [OK]

サイト02:example.net

http://example.net -forwards to- https://example.net [OK]
http://www.example.net -forwards to- https://example.com [ERROR]
https://www.example.net -forwards to- https://example.net [OK]
https://example.net works as expected [OK]

http://www.example.netではなくhttps://example.netに転送するhttps://example.comを希望します

私の.confファイル:

##Site 01: example.com - HTTP NON-WWW .conf##
    <Directory /var/www/html/example.com/public_html>
        Require all granted
    </Directory>

    <VirtualHost *:80>
        ServerName example.com
        DocumentRoot /var/www/html/example.com/public_html

        ErrorLog /var/www/html/example.com/logs/error.log
        CustomLog /var/www/html/example.com/logs/access.log combined

        RedirectMatch permanent ^/(.*) https://example.com/$1
    </VirtualHost>

##Site 01: example.com - HTTPS WWW .conf##
    <IfModule mod_ssl.c>
    <Directory /var/www/html/example.com/public_html>
        Require all granted
    </Directory>

    <VirtualHost *:443>
        ServerName www.example.com
        DocumentRoot /var/www/html/example.com/public_html

        ErrorLog /var/www/html/example.com/logs/error.log
        CustomLog /var/www/html/example.com/logs/access.log combined

        RedirectMatch permanent ^/(.*) https://example.com/$1

        SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
        Include /etc/letsencrypt/options-ssl-Apache.conf
    </VirtualHost>
    </IfModule>

##Site 01: example.com - HTTPS NON-WWW .conf##
    <IfModule mod_ssl.c>
    <Directory /var/www/html/example.com/public_html>
        Require all granted
    </Directory>

    <VirtualHost *:443>
        ServerName example.com
        DocumentRoot /var/www/html/example.com/public_html

        ErrorLog /var/www/html/example.com/logs/error.log
        CustomLog /var/www/html/example.com/logs/access.log combined

        SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
        Include /etc/letsencrypt/options-ssl-Apache.conf
    </VirtualHost>
    </IfModule>

##Site 02: example.net - HTTP WWW .conf##
    <Directory /var/www/html/example.net/public_html>
        Require all granted
    </Directory>

    <VirtualHost *:80>
        ServerName www.example.net
        DocumentRoot /var/www/html/example.net/public_html

        ErrorLog /var/www/html/example.net/logs/error.log
        CustomLog /var/www/html/example.net/logs/access.log combined

        RedirectMatch permanent ^/(.*) https://example.net/$1
    </VirtualHost>

##Site 02: example.net - HTTP NON-WWW .conf##
    <Directory /var/www/html/example.net/public_html>
        Require all granted
    </Directory>

    <VirtualHost *:80>
        ServerName example.com
        DocumentRoot /var/www/html/example.net/public_html

        ErrorLog /var/www/html/example.net/logs/error.log
        CustomLog /var/www/html/example.net/logs/access.log combined

        RedirectMatch permanent ^/(.*) https://example.net/$1
    </VirtualHost>

##Site 02: example.net - HTTPS WWW .conf##
    <IfModule mod_ssl.c>
    <Directory /var/www/html/example.com/public_html>
        Require all granted
    </Directory>

    <VirtualHost *:443>
        ServerName www.example.com
        DocumentRoot /var/www/html/example.net/public_html

        ErrorLog /var/www/html/example.net/logs/error.log
        CustomLog /var/www/html/example.net/logs/access.log combined

        RedirectMatch permanent ^/(.*) https://example.net/$1

        SSLCertificateFile /etc/letsencrypt/live/example.net/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/example.net/privkey.pem
        Include /etc/letsencrypt/options-ssl-Apache.conf
    </VirtualHost>
    </IfModule>

##Site 02: example.net - HTTPS NON-WWW .conf##
    <IfModule mod_ssl.c>
    <Directory /var/www/html/example.com/public_html>
        Require all granted
    </Directory>

    <VirtualHost *:443>
        ServerName example.net
        DocumentRoot /var/www/html/example.net/public_html

        ErrorLog /var/www/html/example.net/logs/error.log
        CustomLog /var/www/html/example.net/logs/access.log combined

        SSLCertificateFile /etc/letsencrypt/live/example.net/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/example.net/privkey.pem
        Include /etc/letsencrypt/options-ssl-Apache.conf
    </VirtualHost>
    </IfModule>
3
iverSUN

仮想ホストの1つにタイプミスがあります。

    ServerName www.example.com
    DocumentRoot /var/www/html/example.net/public_html
    ...
    RedirectMatch permanent ^/(.*) https://example.net/$1

ServerNameが間違っています。タイプミスのため、www.example.netの仮想ホストはまったく設定されていません。つまり、www.example.netはデフォルトの(最初の)仮想ホストにフォールバックします。それが間違ったものにリダイレクトされる理由です。

1