Ubuntu 16.04 LTSでApache2を実行しています。SSL証明書用にCertbotを暗号化します。
2つのドメインがあります。 http
またはhttps
、www
またはnon-www
に関係なく、それぞれhttps://example.com
とhttps://example.net
に着陸したい。
ドメインごとに4つの.confファイルがあります:HTTP WWW
、HTTP NON-WWW
、HTTPS WWW
、HTTPS NON-WWW
。 HTTPS NON-WWW
は常に最終結果でなければなりません。
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]
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>
仮想ホストの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はデフォルトの(最初の)仮想ホストにフォールバックします。それが間違ったものにリダイレクトされる理由です。