Caution: Translations of Quran and Ḥadīth may lead to possible misapplications and misinterpretations. This site is intended for students of sacred knowledge that are proficient in comprehending classical Arabic and have a strong foundation in Islamic sciences. Also note that religious injunctions rely on several aspects beyond what one may glean through reading individual aḥādīth.
quran:2:143

And thus we have made you a just community that you will be witnesses over the people and the Messenger will be a witness over you. And We did not make the qiblah which you used to face except that We might make evident who would follow the Messenger from who would turn back on his heels. And indeed, it is difficult except for those whom Allah has guided. And never would Allah have caused you to lose your faith. Indeed Allah is, to the people, Kind and Merciful.  

Thus, in the same way that We guided you to it, We appointed you, O community of Muhammad (s), a midmost community, excellent and upright, that you might be witnesses to the people, on the Day of Resurrection, that their messengers delivered ˹the Message˺ to them; and that the Messenger might be a witness to you, that he delivered ˹the Message˺ to you, and We did not appoint, make, the direction, for you now, the direction, you were facing, that is the Ka‘ba: the Prophet (s) used to face it in prayer, but when he emigrated he was commanded to face the Holy House ˹of Jerusalem˺, in order to win the hearts of the Jews. He prayed in this direction for sixteen or seventeen months before he changed direction; except that We might know, ˹that it become˺ manifest knowledge, who followed the Messenger, and believed in him, from him who turned on his heels, and returned to unbelief doubting the religion and thinking that the Prophet (s) was confused about this issue; and a number of them apostatised as a result of this— though it, the change of direction, were (wa-in, ‘though’, is softened, and its noun apocopated, originally being: wa-innahā) a grave thing, troublesome for people, save for those, of them, whom God has guided; but God would never cause your faith, that is, your prayers towards the Holy House ˹of Jerusalem˺, to be wasted, but He will reward you for them (the reason that this ˹verse˺ was revealed was that some had asked about the status of those that had died before the change of direction ˹of prayer˺); truly, God is Gentle with, believing, people, Merciful, when He does not let their deeds go to waste (al-ra’fa means ‘intensity of mercy’, and is mentioned first to allow for the end rhyme of the verse ˹with the preceding one˺).
القرآن:٢:١٤٣

وَكَذَٰلِكَ جَعَلْنَاكُمْ أُمَّةً وَسَطًا لِتَكُونُوا شُهَدَاءَ عَلَى النَّاسِ وَيَكُونَ الرَّسُولُ عَلَيْكُمْ شَهِيدًا ۗ وَمَا جَعَلْنَا الْقِبْلَةَ الَّتِي كُنْتَ عَلَيْهَا إِلَّا لِنَعْلَمَ مَنْ يَتَّبِعُ الرَّسُولَ مِمَّنْ يَنْقَلِبُ عَلَىٰ عَقِبَيْهِ ۚ وَإِنْ كَانَتْ لَكَبِيرَةً إِلَّا عَلَى الَّذِينَ هَدَى اللَّهُ ۗ وَمَا كَانَ اللَّهُ لِيُضِيعَ إِيمَانَكُمْ ۚ إِنَّ اللَّهَ بِالنَّاسِ لَرَءُوفٌ رَحِيمٌ  

{وكذلك} كما هديناكم إليه {جعلناكم} يا أمة محمد {أمة وسطا} خيارا عدولا {لتكونوا شهداء على الناس} يوم القيامة أنَّ رسلهم بلَّغتهم {ويكون الرسول عليكم شهيدا} أنه بلغكم {وما جعلنا} صيرنا {القبلة} لك الآن الجهة {التي كنت عليها} أولا وهي الكعبة وكان يصلى إليها فلما هاجر أمر باستقبال بيت المقدس تألُّفا لليهود فصلى إليه ستة أو سبعة عشر شهرا ثم حول {إلا لنعلم} علم ظهور {من يتبع الرسول} فيصدقه {ممن ينقلب على عقبيه} أي يرجع إلى الكفر شكا في الدين وظنا أن النبي في حيرة من أمره وقد ارتد لذلك جماعة {وإن} مخففة من الثقيلة واسمها محذوف أي وإنها {كانت} أي التولية إليها {لكبيرة} شاقة على الناس {إلا على الذين هدى الله} منهم {وما كان الله ليضيع إيمانكم} أي صلاتكم إلى بيت المقدس بل يثيبكم عليه لأن سبب نزولها السؤال عمن مات قبل التحويل {إن الله بالناس} المؤمنين {لرؤوف رحيم} في عدم إضاعة أعمالهم، والرأفةُ شدة الرحمة وقدَّم الأبلغ للفاصلة.