Repository: certbot/certbot

Issue:
Missing 'RewriteEngine on' from vhost auto-configuration in http2https redirection
When redirect all http connections to https setting enabled in vhost autoconfiguration. Installer adds `RewritCond` and `RewriteRule`, but redirection doesn't work, because of missing `RewriteEngine on` -line.

Problem solved by adding this line manually. So please add it to the script. It was all good in former letsencrypt-script.



Discussion:
Any thoughts @sagi?

@dimamedia, can you please provide the http vhost configuration prior to running letsencrypt ?

Prior running the script: 

```
<VirtualHost *:80>
        ServerName domain.fi
        DocumentRoot /data/domain.fi

        <Directory /data/domain.fi/>
                Options FollowSymLinks MultiViews
                AllowOverride None
                Include /data/domain.fi/.htaccess
                Require all granted
        </Directory>

        CustomLog /var/log/apache2/access.log combined
        ErrorLog /var/log/apache2/error.log
</VirtualHost>
```

After running the script:

```
<VirtualHost *:80>
        ServerName domain.fi
        DocumentRoot /data/domain.fi

        <Directory /data/domain.fi/>
                Options FollowSymLinks MultiViews
                AllowOverride None
                Include /data/domain.fi/.htaccess
                Require all granted
        </Directory>

        CustomLog /var/log/apache2/access.log combined
        ErrorLog /var/log/apache2/error.log
RewriteCond %{SERVER_NAME} =domain.fi
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>
```

Manually corrected:

```
<VirtualHost *:80>
        ServerName domain.fi
        DocumentRoot /data/domain.fi

        <Directory /data/domain.fi/>
                Options FollowSymLinks MultiViews
                AllowOverride None
                Include /data/domain.fi/.htaccess
                Require all granted
        </Directory>

        CustomLog /var/log/apache2/access.log combined
        ErrorLog /var/log/apache2/error.log
RewriteEngine on
RewriteCond %{SERVER_NAME} =domain.fi
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>

```

@dimamedia, unfortunately I can't reproduce it. The only thing I haven't tested is your .htaccess file. 
Can you please provide it also?

Here is an included .htaccess file:

```
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteRule sitemap-static\.xml sitemap_static.php [L]
RewriteRule sitemap-categories\.xml sitemap_categories.php [L]
RewriteRule sitemap-products\.xml sitemap_products.php [L]

RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .$ index.php
```

Does `rewriteEngine On` in the .htaccess file somehow implicate its existence. But it runs only in directory based process, but not outside in vhost based. That why vhost needs its own `RewriteEngine On` line. I just reckon.


Resolving patch:
diff --git a/certbot-apache/certbot_apache/configurator.py b/certbot-apache/certbot_apache/configurator.py
--- a/certbot-apache/certbot_apache/configurator.py
+++ b/certbot-apache/certbot_apache/configurator.py
@@ -1177,10 +1177,14 @@ def _is_rewrite_engine_on(self, vhost):
         :type vhost: :class:`~certbot_apache.obj.VirtualHost`
 
         """
-        rewrite_engine_path = self.parser.find_dir("RewriteEngine", "on",
+        rewrite_engine_path_list = self.parser.find_dir("RewriteEngine", "on",
                                                    start=vhost.path)
-        if rewrite_engine_path:
-            return self.parser.get_arg(rewrite_engine_path[0])
+        if rewrite_engine_path_list:
+            for re_path in rewrite_engine_path_list:
+                # A RewriteEngine directive may also be included in per
+                # directory .htaccess files. We only care about the VirtualHost.
+                if 'VirtualHost' in re_path:
+                    return self.parser.get_arg(re_path)
         return False
 
     def _create_redirect_vhost(self, ssl_vhost):


Test patch: