Using my NextCloud after a parent .htaccess rewrite does not work

Nextcloud version:
Operating system and version: Debian GNU/Linux 9
Apache or nginx version: Apache/2.4.25
PHP version: 7.4

I organise most of my projects in a way that redirects the requests internally to a given domain if it matches a folder in /var/www. This works well with many other projects.

See folder structure
  /    <- this is my NextCloud installation
See .htaccess structure
Options -Indexes -MultiViews +FollowSymLinks

# redirect IP access to
RewriteCond %{HTTP_HOST} ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
RewriteRule (.*) [R=301,L]

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

# redirect http to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

# redirect subdomain to folder if exists
RewriteCond %{ENV:PREPENDED} !=1
RewriteCond %{ENV:REDIRECT_STATUS} !^30
RewriteCond /var/www/%{HTTP_HOST} -d
RewriteRule ^ /var/www/%{HTTP_HOST}%{REQUEST_URI} [E=PREPENDED:1]

However, when I do this for NextCloud, accessing to raises an internal server error that says “The server was unable to complete your request.”. When looking at the data/nextcloud.log file I find an error in lib/private/AppFramework/Http/Request.php with the message The requested uri(/) cannot be processed by the script '/').

However, I’m still able to use NextCloud normally from the address How could I fix it so that I can access without having a double URL? I’ve already tried to play with overwrite.cli.url, overwritehost and overwritewebroot with no success.

Is this the first time you’ve seen this error?: yes

Steps to replicate it:

  1. Recreate my folder structure and .htaccess on a fresh Debian 9 install
  2. Install nextcloud on /var/www/
  3. Try to access

The output of your Nextcloud log in Admin > Logging:

Exception: The requested uri(/) cannot be processed by the script '/')

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

$CONFIG = array (
  'instanceid' => '*******',
  'passwordsalt' => '******',
  'secret' => '*******',
  'trusted_domains' =>
  array (
    0 => '',
  'datadirectory' => '/var/www/',
  'dbtype' => 'mysql',
  'version' => '',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost:3306',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'oc_admin',
  'dbpassword' => '*******',
  'installed' => true,
  'maintenance' => false,
  'mysql.utf8mb4' => true,

  'overwrite.cli.url' => ''

The output of your Apache/nginx/system log in /var/log/____: no data is logged at either access.log or error.log.

Can you configure your apache2 virtual hosts?

part: Configure Apache for NextCloud

Because of new software versions you should upgrade to Debian 10:
LTS - Debian Wiki