skip to Main Content

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/mydomain
ServerName mydomain.hu
ServerAlias www.mydomain.hu
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomain.hu [OR]
RewriteCond %{SERVER_NAME} =www.mydomain.hu
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Redirect / https://mydomain.hu/
</VirtualHost>

EDIT:

Modified .conf file with use of answers.

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

2

Answers


  1. VirtualHost must listen on port 443 when using https.

    I usually listen on port 80, add a redirect there and then config the docroot in a separate VirtualHost:

        #this listens on port 80 and redirects to https
        <VirtualHost *:80>
        ServerName mydomain.hu
        ServerAlias www.mydomain.hu
        Redirect 301 / https://mydomain.hu
        </VirtualHost>
    
        #this is to avoid "duplicate" traffic on www
        <VirtualHost *:443>
         ServerName www.mydomain.hu
         Redirect 301 / https://mydomain.hu/
        </VirtualHost>
    
        #here is your docroot so you don't get "Apache2 Ubuntu Default Page"
        <VirtualHost *:443>
        ServerName mydomain.hu
        DocumentRoot /home/www/whatever
        </VirtualHost>
    

    Please note that you should also add access rules and certificates configuration (if needed), otherwise this is not going to work.

    Login or Signup to reply.
  2. You may use a redirect rule like that:

    <VirtualHost *:80>
        ServerName mydomain.hu
        ServerAlias www.mydomain.hu
        RewriteEngine on
        RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [R,L]
     </VirtualHost>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search