FreePBX Outbound Routing and Trunks.
Solution Outbound Routing and Trunks.
Explained by Joe Roper (used with permission)

There are two sections where numbers can be manipulated before passing them out to the outside world. The trunk and the outbound route.
The Trunk

The first thing to note is there is no point whatsoever of putting any anything in the trunk dial rules unless it changes the dial string. The dial rules are there for manipulating the number before sending it onto the carrier.

For instance, many VoIP carriers require calls to be sent to them in what is known as IETF format. For instance, someone in the USA would dial: -

011 44 1179 1179 33 to speak to me in the UK however, your ITSP will only require:-
44 1179 1179 33
In the UK and much of the rest of the world, we simply prefix with a 00, so to call me normally, the user dials 00 44 1179 1179 33, however the carrier requires 441179117933

In the USA, there is a system of numbering called the NANP – the North American Numbering Plan. The plan is made up of three sections:

NPA – the area code [2-9][0-8][0-9]
NXX – The exchange Code [2-9][0-9][0-9]
Station Code – [0-9][0-9][0-9][0-9]

All numbers in the USA follow the above convention. However, the USA international code is 1, therefore your ISP may require you to actually send 1 followed by the remaining 10 digits.

The Outbound Route

The outbound route selects which trunk to use depending on the number dialed by the customer.
It goes in strict order from the top, trying to match the number actually dialed with the pattern in the outbound route. When it matches with a pattern, then it will select the first available trunk connected to that route.

Generally speaking, we try and get all the numbers into the same sort of order before delivering them onto the the trunk. e.g. In the USA, we would be trying to get them into 10 digit NANP format. For the rest of the world, we would be delivering the numbers with the international dialing code, so that if the call goes out via a standard land line, it will simply be delivered as is, whereas if you are going out via a provider, you can add and remove digits to suit the carrier.

The Tools

In both outbound routes and trunks, there are a number of codes that are used to filter or select numbers as well as to do manipulation.


X – Matches any digit from 0 to 9
Z – Matches any digit from 1 to 9
N – Matches any digit from 2 to 9
[123] – Matches only the numbers in the brackets, e.g. 123
[1-3] – Matches a range of digits in the brackets, again 123
[1236-9] – Matches a range of digits as above, 123 678 and 9
. Is a wildcard and matches everything. Asterisk warn against just putting in a . to match with everything – it is better practice to put X. to match everything.

So in the North American numbering plan mentioned previously, we had:-
NPA – the area code [2-9][0-8][0-9]
NXX – The exchange Code [2-9][0-9][0-9]
Station Code – [0-9][0-9][0-9][0-9]

Which translates to
However, for simplicity, it is just written as NXXNXXXXXX

Another example is a UK Cellphone.

The number always starts with a 447, and next digit is a number between 4 and 9, and always with another 9 digits after the 7 – so this number would be selected as 447[4-9]XXXXXXXXX, being lazy, you could just select the number as 447. but there is a danger that people could dial numbers beginning 4470 which are over a dollar a minute, and provide a revenue share for the owner of the number.


Numbers can be manipulated with the following operands.

“+” - Adds a number onto the dialed digits.
“|” _ Subtracts a number from the dialed digits.

So in the USA, to add a 1 onto the front of the NANP dial it is as simple as putting in 1+NXXNXXXXXX before delivering the number to your VoIP carrier. Do this in the trunk dial rule.

In the UK, things are slightly more tricky. When we dial say my number in the UK, we would dial 01179117933, so to get this into IETF format, we have to remove the leading 0 and replace with a 44. This can be achieved by putting in a manipulation rule in the trunk dial rules. 44+0|ZXXXXXXXXX This rule would strip the leading 0 off and add a 44 onto the front where the number was 11 digits long, started with a 0, and the next number was not a 0.

Also in the UK, we can dial locally for instance, 01788 is the Rugby code, and then there are 6 digits, where the first digit is not a 0. So to pass this over a VoIP trunk in IETF format, we need to add 441788 onto the front of the number – this is easily done with 441788+ZXXXXX. However, there is one small problem – directory inquiries in the UK begin with 118 followed by 3 digits, so we would have to put a precise rule in for that as well 118XXX and make sure it went out via a suitable trunk.

The | can be used to help people move from an old PBX, where you do 9 for an outside line, and you still want people to dial with or without a 9. Simply put 9| NXXNXXXXXX and NXXNXXXXXX in the dial patterns box in the outbound route.

This will therefore match any 10 digit NANP number, and any 10 digit NANP number prefixed with 9.


The first job is to create all the trunks, with dial rules to put them into IETF format where necessary, so that the number is delivered as the carrier expects to see it.

The we can move onto Outbound routes.

The first job is to ensure that the emergency services can be called. In the USA, this is 911, or if you are in a panic, it may be 9911 (9 for an outside line) In the UK, it is 999 and in much of Europe 211.

Taking the USA as an example, it is usually preferable to send emergency calls out over the local PSTN so that the caller ID is correct, and the emergency services know where to find you. There are E911 services, but keeping it simple, we assume all Emergency calls are going out over the local PSTN lines.

Operator Services and local calls.

So Create an outbound route, select the first PSTN trunk, and all the others (just in case) and put in:-
911 ; Will call emergency services
9|911 ; Will call emergency services even if they did 9 for an outside line.

Additionally, we can put some other patterns in here for any other numbers which have to go out over the PSTN. IN the UK, we have operator services, which are all 3 or 4 digits long, and never more than 5, so a simple:
ZXXXX ; 5 Digit numbers not starting with a 0
ZXXX ; 4 Digit numbers not starting with a 0
ZXX ; 3 Digit numbers not starting with a 0

In my experience, operator services and numbers that have to be dialed via the PSTN are never more than 5 digits and always start with a 1 to 9.

Then we want to do the same for people who still want to dial 9 for an outside line – as per the legacy system
9|ZXXXX ; 5 Digit numbers not starting with a 0 and prefixed with a 9 for an outside line
9|ZXXX ; 4 Digit numbers not starting with a 0 and prefixed with a 9 for an outside line
9|ZXX ; 3 Digit numbers not starting with a 0 and prefixed with a 9 for an outside line

You may also want to put in something 777|. So you can test your analogue trunks are working correctly, to force a call over the PSTN by prefixing your call with a 777.


The next Outbound route entry will be say for your USA, or national numbers, e.g.

9| NXXNXXXXXX ; NANP numbers, where the user has prefixed with a 9

The leading 1, or whatever has to be added on the front will be added on by the trunk dial rule.
If you are using VoIP, you may want a hidden dial string to force calls over the VoIP trunks, so maybe add 888|. to force calls over VoIP.
International Calls

The final entry, maybe for your international calls.

The length of the dialed digits can vary depending on where you are calling, so this will be the last outbound route.

So in the USA, the international prefix is 011 and the next number will be a number between 1 and 9, so an entry for international routes will be

011Z. Which will match any international number
For the rest of the world that uses 00 as the international prefix, 00Z. Will achieve the same ends.

Again, the 00 or the 011 will be stripped off by the dial rule in the trunk.

Again, we would want to make provision for people dialing 9 for an outside line

9|011Z. Or 9|00Z. As appropriate for your country.

Clearly these rules can be moved around to suit your own particular requirements.

Inbound Routes

It is not well documented, but it is possible to pattern match on inbound routes in the caller ID, this is done by prefixing the Caller ID to be matched with an underscore. So to match all calls that come from Germany, which has a dialcode of 49, simply type _49. in the inbound route caller ID field. This will match all calls which begin with a 49, with anything after it. You can use all of the selection digits to pattern match that you use in the outbound routes.

 Login [Lost Password] 
Remember Me:
 Article Options