Subdomain in subfolder

Hello.

I have installed Nextcloud into a webhosting I have little control of. Everything works - I’m just not able to set up the URL properly. The hosting provider is forcing me to place all the subdomains into a subfolder. The main page is is /www older and all subdomains are in /www/subdomains folder.

The .htaccess folder looks like this:

RewriteCond %{REQUEST_URI} !^subdomains/
RewriteCond %{REQUEST_URI} !^/subdomains/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
RewriteCond %{DOCUMENT_ROOT}/subdomains/%2 -d
RewriteRule (.*) subdomains/%2/$1 [DPI]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^subdomains/[^/]+/(.+[^/])$ /$1/ [R]

The way it works is that

https://mydomain.com/subdomains/nextcloud/index.php

is the same as

https://nextcloud.mydomain.com/subdomains/nextcloud/index.php

and also should be the same as

https://nextcloud.mydomain.com/index.php

Problem is, the first two URL work but the third one gives me ‘Internal Server Error’ and the PHP throws exception at OC\AppFramework\Http\Request->getRawPathInfo().

This is what my config file looks like:

<?php
$CONFIG = array (
  'instanceid' => '*****',
  'passwordsalt' => '*****',
  'secret' => '*****',
  'trusted_domains' => 
  array (
    0 => 'mydomain.com',
    1 => 'nextcloud.mydomain.com',
  ),
  'datadirectory' => '*****/www/subdomains/nextcloud/data',
  'dbtype' => 'mysql',
  'version' => '22.0.0.11',
  'overwrite.cli.url' => 'http://nextcloud.mydomain.com/',
  'htaccess.RewriteBase' => '/',
  'dbname' => '*****',
  'dbhost' => '*****',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => '*****',
  'dbpassword' => '*****',
  'installed' => true,
);
?>

Please note that I can’t edit any apache settings and all my other subdomains work just fine.

Thank you for any help.