I think I remember two typical patterns for successful occupations in history:
(A) The occupied country has at least a similar wealth as the occupier itself.
This allows the occupiers to press enough contributions out of the suppressed to make the occupation pay for itself. France 1940-1944 is an extreme example for a 'profitable' occupation.
(B) The occupation lasts very long (generations).
Only officers, non-commissioned officers, clerks, technical specialists and elite troops are foreigners; the majority of the occupation forces are indigenous troops. This model usually exploits rifts in the occupied society (classes, religions, tribes) and recruits its indigenous troops only from one group. The European colonial powers and their colonies are a good example for this.
- - - - -
Afghanistan looks very differently. It's ridiculously poor in comparison to the involved foreigners and the indigenous forces are neither under control of foreign leaders and specialists nor were they recruited from only one group.
This violation of the well-established patterns may contribute to the explanation of the failure to force central control, peace and order onto Afghanistan so far.
Sadly, neither pattern really helps a country to reduce the risk of becoming occupied. Even the melding of internal rifts wouldn't help much against a multi-year occupation by a peer country.