There exists a client that has many branches round the United kingdom. A few of their branches get their own sites using their own domains. The customer includes a part built system where going to:

www.client.com/aboutus.php?id=branchId

shows the About Us page for your particular branch. Things I require is a method to map specific domain names for their branchId (this really is all saved inside a DB right now) to ensure that

www.client.com/aboutus.php?id=4

maps to

www.branchid4.com/aboutus.php

This works best for me. Unsure in case your domain names can make use of a single general ruleset (ie. grab the ID after which utilize it within the rewriterule), otherwise you will need to repeat the final 3 lines per URL

Options +FollowSymLinks 
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www.)?client.com$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /aboutus\.php\?id=4\ HTTP/
RewriteRule ^.*$ http://www.branchid4.com/about.php? [R=301,L]

You might take the important part from the host title and pass it for your script:

RewriteEngine on
RewriteCond %{QUERY_STRING} !(^|&)branchName=
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.com$
RewriteRule .* $0?branchName=%1 [L,QSA]