In this paper, the octonion matrix equation \(AXB=C\) is studied based on semi-tensor product of matrices. Firstly, we propose the left real element representation and the right real element representation of octonion. Then we obtain the expression of the least squares Hermitian solution to the octonion matrix equation \(AXB=C\) by combining these representations with \(\mathcal {H}\) -representation of the special matrices. In addition, we also put forward the equivalent condition of existence and general expression of the Hermitian solution to the octonion matrix equation \(AXB=C.\) Finally, the validity and stability of our method is verified by numerical experiments.