Inbound Routing to prioritize CID matching over DID matching

In my * setup, using FreePBX, I have a number of indial numbers (DID) and
a number of inbound routes to neatly map each number to a department,


any / any -> front_desk
20000 / any -> ivr1
20001 / any -> ivr2
20002 / any -> sales_rg
20003 / any -> support_rg

If I want the CEO to always get sent to his PA, I expect to

any / CEO_CID -> ceo_pa_ext

However this will never work because any number he could possible call
on already matches a DID/any rule above.

Adding the following;

20000 / CEO_CID -> ceo_pa_ext

does work, and I can add an inbound route for each DID / CID mapping.

This is frustrating for the many CID’s which need matching.

The solution which I’ve found is to split the [ext-did] context into
two levels.

exten => s,1,Goto(ext-did-level2,s,1)
exten => s/CEO_CID, … handle calls from CEO …
… handle remaining CID mappings …

exten => s,1, … handle any/any
exten => 20000, … handle 20000/any
… handle remaining DID mapping …

Now I have prioritized the /CID matches over the DID/ matches
and ensured the CEO gets sent to his PA regardless of which number
he calls.

Naturally this is all removed as soon as free pbx applies its next
config, so is there a freepbx way of doing this at the moment?

Can support for prioritizing Inbound Routes be added?


It won’t work with ‘any’ but it should work with a pattern. For example, assuming all your DIDs come in as 10 digit NA numbers, try something like this and you should not have to modify anything (not the preceding ‘_’ underscore):


That should accomplish it.

Philippe Lindheimer - FreePBX Project Lead
http// - IRC #freepbx

I’ve tried numerous incantations. I’ve deleted “any/any” routes, which under 1.2 would prevent DID/any from matching. I edited
the extensions_additonal.conf to re-order the context in case they were examine sequentially, with no change to the behaviour.

I replaced “any” with _X., _. (thats underscore dot), and _0NNXXXXXX on the DID match lines, the CID match lines and combinations
of the above. (in AUS, we have 0N for the area, and NXXXXXXX for the number).

If asterisk can match a DID, it will always do so in preference to a CID match, which in some instances is desirable.

I’ve started modifying the inbound routing code to generate the two-level matching contexts I described previously, however
I’m sure there are people who want the DID/any match to override the any/CID match (ie, if you call support line, it goes to support
otherwise it is routed based on your CID). My changes will force the ‘any/CID’ match to always route based on your CID even when
you really wanted the support line to be a support line regardless of who called it.

The solution would be to make the ‘order’ of the inbound routes, important, just like the order of the outbound routes is important,
however this requires database changes to include sequence details, and scripting to add ‘up/down’ arrows to rearrange the list, and
some changes to the implementation of the inbound route grouping to define a context per inbound route, with an
exten => s,Goto(next-route,s,1) at the top of each. The last in the list being s,Congestion, or s, depending on whether
the user has an ‘any/any’ rule defined at the end.

Note that an ‘any/any’ rule defined at the top in this fashion would prevent any following inbound routes from ever matching.

All this is probably beyond my ken of php at the moment.

All of this would be overkill if we had one or two lines like any small company. However, we have a block of 100 numbers which we
route between hardware (faxes) users, and departments (ring groups usually).



I fixed the priority problem when you use “any DID”, adding the word “DESC” to the line 1602 in, in /admin/modules/core/, like this:

function core_did_list($order=‘extension’){
switch ($order) {
case ‘description’:
$sql = “SELECT * FROM incoming ORDER BY description,extension,cidnum”;
case ‘extension’:
$sql = “SELECT * FROM incoming ORDER BY extension,cidnum”;
return sql($sql,“getAll”,DB_FETCHMODE_ASSOC);

function core_did_list($order=‘extension’){
switch ($order) {
case ‘description’:
$sql = “SELECT * FROM incoming ORDER BY description,extension,cidnum”;
case ‘extension’:
$sql = “SELECT * FROM incoming ORDER BY extension,cidnum DESC”; // This is the FIX
return sql($sql,“getAll”,DB_FETCHMODE_ASSOC);

With this FIX, FreePBX parse “any DID / any CID” rule at last, because this rule is an “ELSE” rule and must be at the end of the rules list, otherwise any rule after this “ELSE” rule won’t be parsed. It´s simple logic.

Am I clear?

I have Core version



I’m not sure what you are trying to solve by that other than possibly the displayed order. Asterisk resorts everything within a context itself, so it does not matter what order they appear in the context.

A “proper” solution for this forum topic, the desire to have a Any_DID/CID route take priority over a DID/Any_CID route when the call comes in on the DID of the latter requires separate contexts just like we create the catchall in a separate context and put it after the ext-did route.

This has been implemented in 2.5 with a checkbox to mark a give CID only route as a priority and implemented by the introduction of 2 new contexts that are included within ext-did.