FreePBX/Asterisk rule for splitting Telefonica/O2 'To:' header

FreePBX / Asterisk uses the trunk’s context to extract the DID number, which is then matched to incoming routes’ DID numbers.

When using a trunk from Telefonica/O2, the To: header of the incoming SIP INVITE message looks like this:

sip:0891234567;[email protected];user=phone

but when dialling E.164 FQTNs, it can also look like this:

sip:0891234567;[email protected];user=phone

Extracting this using the from-pstn-toheader context, the extracted DID would be 0891234567;phone-context=telefonica.de.

This can’t be matched to DIDs since incoming route DIDs can only consist of numbers, not semicolon.

Therefore, I’ve written this custom context that extracts the DID correctly:

[from-pstn-toheader-o2]
exten => _.,1,Goto(from-pstn,${CUT(CUT(CUT(PJSIP_HEADER(read,To),\;,1),@,1),:,2)},1)

To install this context, add it to /etc/asterisk/extensions_custom.conf in FreePBX or Asterisk and reload Asterisk (if you don’t know how to do that, see How to restart Asterisk in FreePBX, or just restart your FreePBX)

To use this context, go to the trunk’s settings, and set the Context to from-pstn-toheader-o2. Then, apply the configuration.

After that, your DID will be matched correctly (0891234567 for the example above), the ;phone-context=telefonica.de part will be ignored.