Subdomain on hosting with rewrite

I have in root hosting .htaccess
to be able to use different domains or subdomains on one hosting.

RewriteEngine On

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

# subdomain (with or without www)
RewriteCond %{REQUEST_URI} !^subdom/
RewriteCond %{REQUEST_URI} !^/subdom/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
RewriteCond %{DOCUMENT_ROOT}/subdom/%2 -d
RewriteRule (.*) subdom/%2/$1 [DPI]

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

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

and folders:

domain
subdom

In folder subdom i have folder test

In the test folder i have php cake… its work, but the links point to url /subdom/test/contact
I need the links to point to /contact

If the set in app.php:

    'App' => [
        ...
        'base' => '/',
        ...
    ],

Url is ok, but a route matching “contact” could not be found.

Thank You!