Helo! I must make a dynamic virtual host for those subdomains that'll be produced later on using mod_rewrite. All subdomains could be set up prety much same manner, and so i considered using dynamic VH configuration. I would like my document root for every subdomain to become /home/user/public_html/subdomainName.

I have attempted with following configuration, but didn't have success:

<VirtualHost xxx.xxx.xxx.xxx:80>
    # get the server name from the Host: header
    UseCanonicalName Off

    <Directory /home/user/public_html/>
    # ExecCGI is needed here because we can't force
    # CGI execution in the way that ScriptAlias does
      Options FollowSymLinks ExecCGI
    </Directory>

    RewriteEngine On

    # a ServerName derived from a Host: header may be any case at all
    RewriteMap lowercase int:tolower

    #rule that is suposed to set document root of virtual host!??? 
    RewriteRule ^([a-z-]+)\.domain\.com/?(.*) /home/user/public_html/$1/$2

</VirtualHost>

The rule or somethinh appears to become wrong andit does not apply. I have never labored with dynamic VH before and so i have no clue where i am wrong...

rather than dynamic virtual hosts, you may also do that using wildcard dns meaning setting up .yourdomain.tld to suggest for your server.

This can also enable you to get same document root for every domain.

note that you'll want to include ServerAlias *.yourdomain.tld towards the virtual host entry

With RewriteRule you cannot access the domainname. Also employ RewriteCond for your part and employ %1 etc. for backreference.

RewriteCond %{HTTP_HOST} ^([a-z-]+)\.domain\.com$ [NC]
RewriteRule (.*) %1/$1

The only issue is can create a continues loop of prepending the subdomainname string. What exactly I normally do is produce a separate folder for subdomains, like 'subdomains'

RewriteRule ^subdomains - [L]
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.domain\.com$ [NC]
RewriteRule (.*) subdomains/%1/$1

This can be a need many people had before you decide to. So there's an apache module which could do this for you personally mod_vhost_alias http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html

Offers dynamically set up mass virtual hosting