Redirect http -> https Raspberry Pi running Nextcloud

Nextcloud ist running on my Raspberry Pi.

I would like to automatically redirect all requests from the network to https and know that I need to make changes in the .htaccess file, which - amongst other things - looks like this:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} DavClnt
RewriteRule ^$ /remote.php/webdav/ [L,R=302]
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^.well-known/host-meta /public.php?service=host-meta [QSA,L]
RewriteRule ^.well-known/host-meta.json /public.php?service=host-meta-json $
RewriteRule ^.well-known/carddav /remote.php/dav/ [R=301,L]
RewriteRule ^.well-known/caldav /remote.php/dav/ [R=301,L]
RewriteRule ^remote/(.) remote.php [QSA,L]
RewriteRule ^(?:build|tests|config|lib|3rdparty|templates)/.
- [R=404,L]
RewriteCond %{REQUEST_URI} !^/.well-known/(acme-challenge|pki-validation)/.*
RewriteRule ^(?:.|autotest|occ|issue|indie|db_|console).* - [R=404,L]

I have already added the following code to this file at the bottom:

RewriteCond %{HTTPS} !=on
RewriteCond %{ENV:HTTPS} !=on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

but that doesn’t work.

Can anyone explain how to change the file so that all requests are automatically redirected to https? Many thanks in advance for your efforts!

Add this to sudo nano /etc/apache2/sites-available/000-default.conf:

RewriteEngine on
RewriteCond %{SERVER_NAME} =webside.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

After that sudo systemctl restart apache2

Cheers

Unfortunately does not work.

Where should the lines be placed? In virtual host or Directory tag?

before </VirtualHost>
Did you changed the URL od the RewriteCond %{SERVER_NAME} = your url?

Yup, did change it, also url …still does not work :frowning:

Should I also put that code into .htaccess?

a2enmod rewrite

Is already enabled?

Doing that can cause overwrites with future updates (when there are some changes in .htacess).
Changes in Virtual Host file, are for all virtual hosts (all websides in the end).

a2enmod rewrite

is enabled

Did not change .htaccess …

does not work :frowning:

Do you have apache2 installed at all? Or lighttpd?

Try this line
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R,L]
in .htaccess (instead of your line)

Does not work in .htaccess :frowning:

But I could solve the problem on my own…

in .htaccess:

<IfModule mod_rewrite.c>

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_USER_AGENT} DavClnt

the first three lines did the thing for me :slight_smile:

Thanks to everyone who helped me!!!

1 Like