Programmatically change the Base URL?
You can do that with:

hook_url_outbound_alter

Keep in mind all URLs run through that function so your hook implementation should be as fast as possible.

Example from locale module:


  Rewrite outbound URLs with language based prefixes:locale_url_outbound_alter  


See also: Language Negotiation API