0

Apache2 is not redirecting by default http to https on one of the rules when domain starts with http://www. My domain using certbot.

000-default.conf - disabled

https://domain.com/- loading correctly

https://www.domain.com/ - loading correctly

http://domain.com - redirect correctly

http://www.domain.com - loads Apache2 Ubuntu Default Page

I tried many combinations, please see my current vhost files setup.

My /var/www/html/.htaccess

RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [NC,R=301,L]

My /etc/apache2/sites-available/my_project.conf

<VirtualHost *:80>

        ServerName domain.com
        ServerAlias www.domain.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

RewriteEngine on
RewriteCond %{SERVER_NAME} =www.domain.com [OR]
RewriteCond %{SERVER_NAME} =domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

My /etc/apache2/sites-available/my_project-le-ssl.conf

<IfModule mod_ssl.c>
<VirtualHost *:443>

        ServerName domain.com
        ServerAlias www.domain.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        #django app directory setup

        #django app WSGI setup

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/www.domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.domain.com/privkey.pem
</VirtualHost>
</IfModule>
<IfModule mod_ssl.c>
<VirtualHost *:80>

Maybe I am missing something, could you point me in right direction?

  • It is better to use Redirect directive for this task: https://askubuntu.com/a/1031460/566421 – pa4080 Nov 29 '19 at 11:14
  • @pa4080 thanks for advice but looks like it doesn't resolve issue, I changed to Redirect directive in both ways (with commas and without it in hostnames), set global ServerName variable outside VirtualHost also followed Apache documentation and tried few options, SO frustrating! :( – MrDominikku Nov 29 '19 at 13:10
  • @pa4080 looks like there was some port conflict in my_project-le-ssl.conf file, two vhosts each with different port which made server to incorrect redirect – MrDominikku Nov 29 '19 at 18:04

0 Answers0