Site domain tranformed as additional URL parameter

I have the same web infrastructure for different sites. In my reality different sites are representation of different content from different databases on the same web infrastructure but I want to keep it hidden from users.

I want that domain name is transferred as additional parameter site in my URL but this has to be hidden from users. For example one domain URL http://www.domain1.com should be transferred into http://www.maindomain.com/?site=domain1, other domain http://www.domain2.com into http://www.maindomain.com/?site=domain2.

Other URL parameters should be preserved as well. For example: http://www.domain3.com/index.php?param1=100&param2=200 should be transferred into http://www.maindomain.com/index.php?site=domain3&param1=100&param2=200

All this should be hidden from user – user should only see:

http://www.domain3.com/index.php?param1=100&param2=200

but in background the following should be processed:

http://www.maindomain.com/index.php?site=domain3&param1=100&param2=200

How to achieve this?