J'utilise Apache2 sur Ubuntu 16.04 LTS et Encrypt Certbot pour les certificats SSL.
J'ai deux domaines. Indépendamment de http
ou https
, www
ou non-www
, je souhaite qu’ils atterrissent respectivement à https://example.com
et https://example.net
.
J'ai 4 fichiers .conf par domaine: HTTP WWW
, HTTP NON-WWW
, HTTPS WWW
, HTTPS NON-WWW
. HTTPS NON-WWW
devrait toujours être le résultat final.
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]
Je voudrais que http://www.example.net
soit transféré à https://example.net
, pas https://example.com
Mes fichiers .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>
Vous avez simplement une faute de frappe dans l'un de vos hôtes virtuels:
ServerName www.example.com
DocumentRoot /var/www/html/example.net/public_html
...
RedirectMatch permanent ^/(.*) https://example.net/$1
La ServerName
est fausse. Vous n'avez pas du tout d'hôte virtuel configuré pour www.example.net à cause de la faute de frappe. Cela signifie que www.example.net revient au premier hôte virtuel par défaut. C'est pourquoi il est redirigé vers la mauvaise chose.