Asterisk routing help needed

I now have 2 servers, pbx on 10.1.0.50 and pbx1 on 10.1.0.153

I have configured IAX and they talk to each other quite happily.

This is the scenario:

Building A has the ISDN lines and pbx
Building B has pbx1 and a VPN connection to building A

I want all EXTERNAL calls from building B (pbx1) to go via the VPN to building A and out of pbx.

I want all INTERNAL calls made in building B NOT to go via building A but to be handled by pbx1.

Can this be done and if so how?

It depends where the extensions are registered. If you have extensions 6601 and 6602 registered on PBX1 and you have extensions 6701 and 6702 on PBX then in order to call these phones you will need to establish a connection with the PBX the extension is registered to.

So from 6601 a call to 6701 will need to go through PBX. A call from 6601 to 6602 can be handled entirely on PBX1 based on the extensions.conf configuration.
A call from 6701 to 6601 will need to go through PBX1, and so on.

On PBX1 you need to modify the extensions.conf so that its registered extensions are connected directly. External numbers and extensions registered on PBX should be passed to PBX.

On PBX you would have all its registered extensions connected directly. Extensions registered on PBX1 will need to be passed to that server. External calls should be passed to the ISDN.

[/code]

you may think about it like this:
assumes that all external calls should start with digit 9 then in your default context (the context that all parties in pbx1 are redirected to) create an extension like this:
exten => _9X.,1,dial(iax2/PBXIaxTrunk/${EXTEN:1})
where PBXIaxTrunk is whatever iax trunk you have created between pbx, pbx1.
Now in pbx, assumes that the context for your iax trunk is incoming, then under this context get all incoming calls by using extension (s) and redirect the them to your ISDN line.
All other internal calls in pbx1 could be handled as you like, as far as they don’t have the 9 prefix.