Assalamualaikum ustaz. When I was praying, I suddenly realize that I was facing the wrong qibla. What should I do? Should I restart my prayer again or is it fine if I just turn and face the correct qibla direction?
Waalaikumussalam wrt. wbt.,
Alhamdulillah, praise and gratitude to Allah SWT for His countless blessings for us all. Praise and salutations to our beloved Prophet Muhammad PBUH, his family, companions, and all those who follow his footsteps until the Final day.
Definition of qibla
Lexically, qibla means direction (al-jihah) where we face it and according to the syarak terminology, it is understood as the Kaaba in absolution without having any other meanings. 
There is no khilaf among scholars that facing the qibla is one of the valid conditions of prayer for those who are capable. 
Facing the qibla as a valid condition of prayer
According to the evidence from the book al-Fiqh al-Manhaji, it is stated: What is meant by qibla is Kaaba, while facing qibla means the Kaaba must be in front of him. The evidence for this condition is the statement of Allah SWT:
وَمِنْ حَيْثُ خَرَجْتَ فَوَلِّ وَجْهَكَ شَطْرَ الْمَسْجِدِ الْحَرَامِ ۚ وَحَيْثُ مَا كُنتُمْ فَوَلُّوا وُجُوهَكُمْ شَطْرَهُ
“Turn your face toward al-Masjid al-Haram. And wherever you [believers] may be, turn your faces toward it,” 
Likewise, in a hadith, Rasullullah PBUH said to the person he is teaching the methodologies of prayer:
إِذَا قُمْتَ إِلَى الصَّلاَةِ فَأَسْبِغِ الْوُضُوءَ، ثُمَّ اسْتَقْبِلِ الْقِبْلَةَ فَكَبِّرْ
“When you get up to pray, perform the ablution completely, and then turn towards the Qibla and recite takbir (Allah o Akbar =Allah is the Most Great).” 
The meaning of Masjid al-Haram in the verse and the meaning of qibla in the hadith is qibla. People who want to pray regardless of whether they are near the Kaaba and can see it or far away and it is impossible to see it. A person who is near the Kaaba is obligated to face the Kaaba with certainty and those who are far away are obligated to face it following the strong guides when there isn’t any certain and accurate guide. 
Facing the Kaaba means one’s chest faces the Kaaba not the head for a person who is praying while standing and sitting; with his head and chest for a person who is praying on his side; and with his head and toes for someone who is praying in a supine position. 
For those who are far away from the Kaaba or those who are obstructed from seeing the Kaaba, it doesn’t affect their prayer if the position is a little bit to the right or left from the ain (physical matter) of the Kaaba, for it is a slight mistake as long as it doesn’t deviate away from the Kaaba direction. 
In madhhab al-Syafie, the ways to determine the Kaaba direction are arranged in four levels of priorities, which are:
- One’s own certain knowledge such as a person who can see the Kaaba itself or a blind person who touches the Kaaba.
- Information from a trustworthy knowledgeable person.
- Ijtihad such as using a compass and others.
- A person who follows (taqlid) with the ijtihad of another person. 
Realizing that one is facing the wrong qibla in prayer
If a person who is praying realizes that he is facing the wrong qibla direction, then scholars of madhhab Syafie are divided into two qaul:
First: Restart the prayer with the new ijtihad.
Second: It isn’t necessary to restart the prayer and it is sufficient to turn and face the new ijtihad and continue praying (facing the direction of the new ijtihad).
The second opinion is the most sahih opinion in madhhab Syafi’i. This is as such even if a person changes direction four times in a prayer due to his changing ijtihad, his prayer is still valid. 
To conclude, if a person realizes that he is facing the wrong qibla when he is praying (when there is a certain guide), then he should turn and face the new direction.
Furthermore, if someone sees another person praying while facing the wrong qibla when he knows for certain the right direction, then he should tell the person praying and the person praying is obligated to turn if he is certain that the correction is true and trustworthy.
 See Mughni al-Muhtaj, 1/142
 See al-Mausu’ah al-Fiqhiyyah al-Kuwaitiyyah, 32/302
 (Surah al-Baqarah: 150)
 Narrated by al-Bukhari (5897) and Muslim (397)
 See al-Fiqh al-Manhaji, 1/127-128
 See Kasyifah al-Saja, pg. 197; al-Taqrirat al-Sadidah, pg.200
 See al-Mu`tamad fi al-Fiqh al-Syafie, 1/217
 See I`anah al-Talibin, 1/145
 See al-Majmu’ Syarh al-Muhazzab, 3/221