I have tried a few examples of part of my question, but none would work either.
I want to use rewrite to change a url from ...
randomsub.domain.com/qwerty
to
domain.com/folder/index.php?domain=randomsub&id=qwerty
The examples I tried just for the subdomain part wouldnt work, as it would show Cant connect to server ...
Any assistance would be appreciated
I think this is roughly what you are looking for, if you really want to make an internal rewrite, as you write in your question. An alternative would be an external redirection which would also change the URL visible in the browser.
In general you first need to configure a default host inside your http servers host configuration section. That host has to respond to all incoming requests to those random host names ("subdomains") you want to react to. Inside that default host you need to implement your rewriting rule:
RewriteEngine on
RewriteCond %{QUERY_HOST} ^(.+)\.example\.com$
RewriteRule ^/?(.*)$ /folder/index.php?domain=%1&id=$1 [L]
That rule will work likewise in the http servers host configuration or in a dynamic configuration file (".htaccess" style file). You certainly should prefer the first option for various reasons.
A general remark about this: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).