Wouldn't this have to be handled at the Apache level or even possibly higher up?
You're trying to mess with domain addresses - and the domain address tells one computer how to get to another computer. I'm no networking expert but I think what you're trying to do falls outside the boundaries of php on a webserver, because you generally can't just go to (whatever).address.com.
A URL is an address to a server. Typically if you change the prefix of an address, one of two things happens. Take "custom.vbulletin.com/org".
If you go to custom.vbulletin.com - you get redirected to
www.vbulletin.com - not by php, but either by the dns on vbulletin's server, or by apache on vbulletin's server. One or the other has been set up to handle incoming requests to (unknown).vbulletin.com and redirect them to www. This isn't a php function - this happens before the browser ever gets to the pages.
If you try to go to custom.vbulletin.org on the other hand, you get a page cannot be found. Why? Because no redirect has been set up at a higher level to send ????.vbulletin.org to www. That redirect missing means the browser can't find the server or - if you found the server, the server didn't know what to do with you.
So I don't think what you can do is within the bounds of php *inside* the forums because the browser has to find the forum first. At a minimum dns and/or apache would have to know to pass (unknown).yourdomain.com to www and THEN you'ld have to examine the referrer url inside php and look up the right user.