Q:

Mod rewrite subdomains based on query string

I gave localhost an alias: 'myhost'. Entering 'myhost' in my address bar shows me /var/www/index.php. So far so good. Set up an apache virtual host on /var/www and called it 'myhost', then added three aliases: '*.myhost' '*.myhost.* 'myhost.*' the idea being that it would work like so:
myhost = site index/landing page/general stuff
user.myhost = general stuff specific to the user
myhost.topic = topical stuff not specific to a user
user.myhost.topic = topical stuff specific to a user
but all the data is loaded dynamically so the actual urls would be more like:
myhost/index.php?user=(empty or name of user)&host=myhost&app=(empty or topic)
I've spent two days trying to figure out the rewrite rules for this and no joy. Any help would be greatly appreciated.
EDIT: Also, do I have to add user1.myhost, user2.myhost, myhost.topic1, myhost.topic2, user1.myhost.topic1, user2.myhost.topic1, user1.myhost.topic2, user2.myhost.topic2, etc to my hosts file to make this work? If so, isn't there a better way?

A:

You can use these rewrite rules to redirect all domains to myhost with a host parameter. 
RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST} myhost
RewriteRule .* - [L]

RewriteCond %{QUERY_STRING} ^.+$
RewriteRule ^(.*)?(.*)$ $1?$2&host=%{HTTP_HOST} [L]

RewriteRule ^(.*)$ http://myhost/$1?host=%{HTTP_HOST} [L]

You can also get domain name in your app (for example $_SERVER["SERVER_NAME"] in php) and use this value. So you dont need an extra host parameter for it.
You can install an easy-to-use dns server (like tinydns) and set wildcard subdomains like *.myhost etc. But user2.myhost.topic2 is not a valid domain. If your domain is "example.com", you subdomains should be "topic1.example.com" or "user1.topic1.example.com". You cant use "user1.example.topic1" as a domain in internet.

