Redirect HTTP to HTTPS with VirtualHost

I have Let’s Encrypt SSL certificate for exampledomain.com. www.exampledomain.com redirects to https://exampledomain.com, but exampledomain.com gives “Apache2 Ubuntu Default Page”. I use following .conf file for domain in sites-available folder:

<VirtualHost *:80>
DocumentRoot /var/www/html/mydomainamehu
ServerName mydomainame.hu
ServerAlias www.mydomainame.hu
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomainame.hu [OR]
RewriteCond %{SERVER_NAME} =www.mydomainame.hu
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Redirect / https://mydomainame.hu/
</VirtualHost>

<VirtualHost *:80>
    ServerName mydomainame.hu
    ServerAlias www.mydomainame.hu
    RewriteEngine on
    RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [R,L]
 </VirtualHost>

<VirtualHost *:443>
 ServerName mydomainame.hu
 Redirect 301 / https://mydomainame.hu/
</VirtualHost>

<VirtualHost *:443>
ServerName mydomainame.hu
DocumentRoot /var/www/html/mydomainamehu
</VirtualHost>

You show four virtual hosts:

The first competes with the second and the third competes with the fourth for similar coverages.
From that set, It seems you only need two (first and third).
But even when using all four, you fail to show coverage for https://www.mydomainname.hu/
One and two cover both names for http.
Three and for cover https - but only for the root domain.
And (if/when used) the third redirects to itself - endless loop.

I use first and third, but get error message in browser: ERR_TOO_MANY_REDIRECTS

Hi,

The thrid one is a https virtual host, and the 403 means no matter what specific page you visit in https protocol (of this site), it will always return you to the front page, and, the front page is going to sent you to the front page… (Which is why it’s a infinity loop)

Here is the (seemly) correct version: (I’ve tried to optimize the http redirection)

<VirtualHost *:80>
DocumentRoot /var/www/html/mydomainamehu 
ServerName mydomainame.hu 
ServerAlias www.mydomainame.hu 

RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS

RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

</VirtualHost>

<VirtualHost *:443>
ServerName mydomainame.hu 
DocumentRoot /var/www/html/mydomainamehu 
</VirtualHost>

Thank you

Sorry, the third should NOT be used.
So first and (a modified) fourth may work better.
Modified to include the server alias name “www”

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.