I have a server, that should redirect all traffic to a NON-WWW, HTTPS page like https://website.com But not all traffic gets redirected.
What do I expect and what do i get?
For redirecting I use the following Apache2 .conf files in etc/apache2/sites-available:
front.conf
<VirtualHost *:80>
ServerName website.com
ServerAlias www.website.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/...
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.website.com [OR]
RewriteCond %{SERVER_NAME} =website.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
# RewriteCond %{HTTPS} off [OR]
# RewriteCond %{HTTP_HOST} ^www\. [NC]
# RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
# RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</VirtualHost>
front-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName website.com
ServerAlias www.website.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/...
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine On
# Too many redirects
# RewriteCond %{HTTP_HOST} ^www\. [NC]
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Include /etc/letsencrypt/...
SSLCertificateFile /etc/letsencrypt/...
SSLCertificateKeyFile /etc/letsencrypt/...
</VirtualHost>
</IfModule>
I also tried the commented RewriteCond/-Rule, but nothing leads to the expected behaviour. Have I missed anything important? I spent long time on searching different approaches, but nothing seems to work the way I need.
Thanks in advance.
The solution was a mix of the following 3 components:
The solution was to rename the file front.conf to 000-front.conf and front-le-ssl.conf to 000-front-le-ssl.conf. As apache2 reads the config files alphabetically, it comes before all my other files (api.conf, ...)
hope this solution helps others