The quantum Rabi model is a widespread description for the coupling between a two-level system and a quantized single mode of an electromagnetic resonator. Issues about this models gauge invariance have been raised. These issues become evident when the light-matter interaction reaches the so-called ultrastrong coupling regime. Recently, a modified quantum Rabi model able to provide gauge-invariant physical results in any interaction regime was introduced [Nature Physics 15, 803 (2019)]. Here we provide an alternative derivation of this result, based on the implementation in two-state systems of the gauge principle, which is the principle from which all the fundamental interactions in quantum field theory are derived. The adopted procedure can be regarded as the two-site version of the general method used to implement the gauge principle in lattice gauge theories. Applying this method, we also obtain the gauge-invariant quantum Rabi model for asymmetric two-state systems, and the multi-mode gauge-invariant quantum Rabi model beyond the dipole approximation.