子文件夹的RewriteRule

I have a htaccess file where I redirect every http request to https. http://sample.com/a/b/c -> https://sample.com/a/b/c

That works, so far so good. Now I have a subfolder as a subdomain. htdocs/api accessible via http://api.sample.com/x/y/z

Whenever someone requests the api subdomain I want it to redirect to https and push everything to the index.php script at api.sample.com but not redirect it.

http://api.sample.com/path/to/endpoint should be handled in the index.php but it redirects to https://api.sample.com/index.php and my path is gone.

This is what I have so far.

DirectoryIndex index.php

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^api\. [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/index.php [L,QSA]

Example: index.php in htdocs/api

$uri = parse_url($_SERVER['REQUEST_URI']);
print $uri['path'];

I visit GET http://api.sample.com/path/to/endpoint and want "/path/to/endpoint" being printed out.

Whats wrong with my RewriteRule?

Thanks in advance

Here example of .htaccess

<IfModule mod_rewrite.c>
RewriteEngine on
 # To append a query string part in the substitution string
RewriteRule ^([0-9a-z_/\-]+)/$ index.php\?p=$1 [QSA]
RewriteRule ^([0-9a-z_/\-]+)$ index.php\?p=$1 [QSA]

php_flag allow_url_include on
</IfModule>

Here my index.php and read file from a subfolder

session_start();
if (isset($_GET['p'])) {
    if (isset($_GET['p'])) {
        $request=explode('/',$_GET['p']);
        include 'subfolder/'.$request[0].'.php';
    }
}
else {
    include 'subfolder/default.php';
}

Where subfolder/ is name of your folder name

Here samples of site i parse https://andre.cloudmanado.store/