skip to Main Content

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


  1. Chosen as BEST ANSWER

    Thanks, the solution was a mix of the following 3 components:

    • Separate Non-WWW and WWW VirtualHosts (as mentioned above)
    • SSL Redirect (also as mentioned above)
    • default server I needed to rearrange the served pages on apache2 to make the "website.com" the default

    Problem solved!


  2. 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:

    1. 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.

      <VirtualHost *:80>
          ServerName website.com
          ServerAdmin webmaster@localhost
          DocumentRoot /var/www/...
      
          RewriteEngine on
          RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
      </VirtualHost>
      
      <VirtualHost *:80>
          ServerName www.website.com
          Redirect 301 / https://website.com/
      </VirtualHost>
      

      Adjust the SSL VirtualHost similarly.

    2. 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.

      <VirtualHost *:443>
          ServerName website.com
          ServerAdmin webmaster@localhost
          DocumentRoot /var/www/...
      
          RewriteEngine On
          RewriteCond %{HTTP_HOST} ^www. [NC]
          RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
      
          Include /etc/letsencrypt/...
          SSLCertificateFile /etc/letsencrypt/...
          SSLCertificateKeyFile /etc/letsencrypt/...
      </VirtualHost>
      

      Make sure to disable the commented-out redirect rule in your SSL VirtualHost.

    3. Check Configuration:
      After making changes, ensure to restart Apache to apply the new configuration.

      sudo service apache2 restart
      
    4. 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.

    5. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search