Redirect WWW to NON-WWW and HTTP to HTTPS with Apache2 on Ubuntu Server

65 Views Asked by At

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.

1

There are 1 best solutions below

2
georgleb On

The solution was a mix of the following 3 components:

  • Separate Non-WWW and WWW VirtualHosts
<VirtualHost *:80>
        ServerName website.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/...
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        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>
  • SSL Redirect
<IfModule mod_ssl.c>
        <VirtualHost *:443>
                ServerName website.com
                ServerAdmin webmaster@localhost
                DocumentRoot /var/www/
                ErrorLog ${APACHE_LOG_DIR}/error.log
                CustomLog ${APACHE_LOG_DIR}/access.log combined

                RewriteEngine On
                RewriteCond %{HTTP_HOST} ^www\. [NC]
                #RewriteRule ^ https://website.com%{REQUEST_URI} [R=301,L]
                RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

                Include /etc/letsencrypt/
                SSLCertificateFile /etc/letsencrypt/
                SSLCertificateKeyFile /etc/letsencrypt/
        </VirtualHost>
</IfModule>
  • default server I needed to rearrange the served pages on apache2 to make the "website.com" the default

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