Subdomain in subfolder


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

is the same as

and also should be the same as

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:

$CONFIG = array (
  'instanceid' => '*****',
  'passwordsalt' => '*****',
  'secret' => '*****',
  'trusted_domains' => 
  array (
    0 => '',
    1 => '',
  'datadirectory' => '*****/www/subdomains/nextcloud/data',
  'dbtype' => 'mysql',
  'version' => '',
  'overwrite.cli.url' => '',
  '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.