كتاب الطهارة
3  
Purification
(3a)
Chapter: How to act when relieving oneself - Section 1
(2)
باب آداب الخلاء - الفصل الأول
Mishkat al-Masabih 334, 335
Abu Ayyub al-Ansari reported God’s messenger as saying, "When you go to the toilet neither face nor turn your back to the qibla [Ka'ba in Mecca], but turn towards the east or the west.” (Bukhari and Muslim.) The shaikh and imam Muhyi as-Sunna said that this tradition applies to the desert, but that in a built-up area it does not matter, on account of what is transmitted from ‘Abdallah b. ‘Umar who said, "I went up to the roof of Hafsa’s house for some purpose and saw God's messenger relieving himself facing Syria, with his back to the qibla.” (Bukhari and Muslim.)
عَنْ أَبِي أَيُّوبَ الْأَنْصَارِيِّ قَالَ: قَالَ رَسُولُ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ: «إِذَا أَتَيْتُمُ الْغَائِطَ فَلَا تَسْتَقْبِلُوا الْقِبْلَةَ وَلَا تَسْتَدْبِرُوهَا وَلَكِنْ شَرِّقُوا أَوْ غَرِّبُوا» قَالَ الشَّيْخ الإِمَام محيي السّنة : هَذَا الْحَدِيثُ فِي الصَّحْرَاءِ وَأَمَّا فِي الْبُنْيَانِ فَلَا بَأْس لما رُوِيَ:

عَن عبد الله بن عمر قَالَ: ارْتَقَيْتُ فَوْقَ بَيْتِ حَفْصَةَ لِبَعْضِ حَاجَتِي فَرَأَيْتُ رَسُولَ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ يقْضِي حَاجته مستدبر الْقبْلَة مُسْتَقْبل الشَّام

Reference : Mishkat al-Masabih 334, 335
In-book reference : Book 3, Hadith 47