There is not a rule, as far as I know. Instead of pronouncing a t with the tip of the tongue on the palate it is pronounced by stopping the breath in the throat. It only occurs if the t is in the middle or the end of the word. So, "bottle" often has a glottal stop, but total has a normal t at the beginning and a glottal stop in the middle.
It happens with other consonants as well, the k in talking.