Praise be to Allah.
There is nothing wrong with the worshipper shifting his weight from one leg to the other sometimes, if he is standing for a long time in prayer, so as to give himself a rest. That is so long as he does not put one foot in front of the other, so that one foot is in line with the people and the other is in front of or behind it. This action is permissible for the one who has an excuse, but it is makrooh (disliked) if one does not have an excuse.
An-Nawawi (may Allah have mercy on him) said:
If a person puts his weight on one leg, his prayer is valid although it is makrooh; if he has an excuse, then it is not makrooh.
And he (may Allah have mercy on him) also said:
It should be noted that it is makrooh for one who is in good health to put his weight on one leg, but his prayer is still valid.
Rawdat at-Taalibeen, 1/234
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said:
With regard to shifting one’s weight from one leg to another, there is nothing wrong with it, especially when standing for a long time (in prayer), but it should be done without putting one foot in front of the other; rather the two feet should be in line with one another. And it should not be done too much. End quote.
Ash-Sharh al-Mumti‘, 3/224
And Allah knows best.