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.
2
Answers
Thanks, the solution was a mix of the following 3 components:
Problem solved!
It looks like you are trying to configure Apache to redirect all traffic to a non-www HTTPS page. Your Apache configuration appears to be mostly correct, but there might be a couple of issues. Here are some suggestions:
Separate Non-WWW and WWW VirtualHosts:
Instead of combining both non-www and www configurations in the same VirtualHost block, create separate VirtualHost blocks for non-www and www versions. This can help avoid conflicts.
Adjust the SSL VirtualHost similarly.
SSL Redirect:
In the SSL VirtualHost, you may need to include a separate redirect rule to ensure all requests are redirected to the non-www version. You can use a different approach for HTTPS redirection.
Make sure to disable the commented-out redirect rule in your SSL VirtualHost.
Check Configuration:
After making changes, ensure to restart Apache to apply the new configuration.
Clear Browser Cache:
Sometimes, browsers cache redirects. Clear your browser cache or try in a new incognito/private window to ensure you are not seeing cached results.
Check for Other Configuration Files:
Ensure that there are no conflicting configurations in other Apache files, such as
.htaccess
files or additional configuration files included in your Apache setup.After making these adjustments, your Apache configuration should redirect all traffic to the non-www HTTPS version as expected.