Multisite Customized URL

hi, i have below scenario. i have a multisite installation i have request like this example.com/?UniqueCode i want to rewrite it ot subdomain.example.com, subdomain will be based on UniqueCode (i will added it to wordpress DB). what is the best way to accomplish this.