Nextcloud not being forced to https connection

I have Nextcloud setup on a LAMP server on Ubuntu 16.04 Server. I am using Let’s Encrypt as my CA and initiated my cert with their certbot tool. I set it to force a redirect to a secure HTTPS connection, however it won’t establish an HTTPS connection unless I specify HTTPS. In my apache config files for VirtualHost *:80 I set rewrite conditions that worked for me before on Raspbian along with the lets encrypt certs. Here’s what’s in my nextcloud.conf file in /etc/apache2/sites-available (and symlinked to sites-enabled)

    ReWriteEngine on
    ReWriteCond %{SERVER_PORT} !^443$
    ReWriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]

Hard to say like this. Apache itself recommends to just use the redirect statement for this purpose:
https://wiki.apache.org/httpd/RedirectSSL

I’m not sure why you are using the NC-flags?

I’m just using the NC flags because that’s what I have been seeing everywhere, honestly I know very little about configuring Apache, I’m very new to web hosting

I tried the redirect statement once before and that didn’t work, what ended up working on my Pi at least was the ReWriteEngine and the other 2 lines. I will try what you sent though, I forgot about that

Thank you for sharing the link! So here is what I have found from installing this on Raspbian and Ubuntu 16.04 Server:

The redirect is working like I want it to work with the 3 lines I showed above on my Pi, however on Ubuntu Server I didn’t add those 3 lines, instead I did this:

Redirect permanent / https://example.com/

Works like a charm

EDIT:

Spoke too soon. Doesn’t work, same problem persists

In my vhost I have:

Redirect permanent / https://cloud.domain.com/ and after a server reload that works.

But I do also have this in my config.php

'overwrite.cli.url' => 'https://cloud.domain.com:443',
'overwriteprotocol' => 'https',

Because I too was originally struggling.

That’s not cutting it for me either…I saw what Let’s Encrypt added to my conf file

RewriteEngine on
RewriteCond %{SERVER_NAME} =htnextcloud.duckdns.org
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

I followed your instructions and commented those out too with no luck. My other computer that is running nextcloud right now just has those 3 lines and it’s doing fine

This is very strange, you are sure you do it in the right vhost?

You can test the redirection on the terminal as well, a curl http://example.org should give you a redirect notification.

Currently I get a success with https://example.org, but with http://example.org I get:
curl: (52) Empty reply from server

Found it! It’s a networking problem. I could have sworn I port forwarded port 80 on my router but I guess it didn’t stick? Not sure why the config on my router didn’t save, but both of your answers work. I did what JasonBayton said and I don’t have any issues, I commented the RewriteEngine lines I showed in my comments