What is the ruling of visiting graves in general, and visiting the grave of the Prophet in particular? Is it permissible to travel (shadd al-rihal) with the intention of visiting the grave of the Prophet and the graves of the righteous?
The answer to this question is in two parts. The first part is the ruling of visiting the graves of the righteous from amongst the Muslims, and the grave of the Prophet. The second part is the ruling of traveling with the intention of visiting the grave of the Prophet or the graves of the righteous.
Firstly, the ruling of visiting the graves of Muslims and the grave of the Prophet:
The entire community of Muslims is in agreement that visiting graves is permissible. All of the scholars are agreed that it is preferable for men, and the Hanafis consider it to be preferable for women as well, while the majority hold it to be permissible but disliked due to the tenderness of their hearts and their inability to be patient. The evidence for its preferability is the saying of the Prophet, "I used to forbid you to visit graves, [but now] visit them, for it is a reminder of the afterlife." According to the majority of scholars there is no dislike in women visiting the grave of the Prophet, rather it is preferred for them to visit him as well as the graves of the other prophets. This exception is based on the generality of the evidence for visiting the Prophet.
As for visiting the grave of the Prophet, no intelligent Muslim can be unaware of its value. How would it be if the Messenger of God was among us now and had not passed on to his Lord, would we go to visit him? Surely we would not hesitate to visit him, and visiting the Prophet after his passing is done by visiting his noble grave.
The Muslim community has come to a consensus concerning the permissibility of visiting the Prophet. The majority of scholars who are qualified to issue fatwas in their schools of jurisprudence have adopted the position that it is a preferred sunna to visit the Prophet.
Some of the realized ones (al-muhaqiqun) have said that it is an affirmed sunna near to being mandatory, which is the fatwa according to a segment of the Hanafis, while the Maliki jurist Abu 'Umran Musa ibn 'Isa considered it mandatory.
They based this opinion on various pieces of evidence among which is the verse from the Quran And if, when they had wronged themselves, they had but come to you and asked forgiveness of God, and asked forgiveness of the messenger, they would have found God Forgiving, Merciful [4:64]. This verse is absolute, there is no textual or rational evidence that particularizes, there is nothing that relegates it to the worldly life of the Prophet, therefore it remains effective until the end of time.
Usually, the import of the Qur'an is in the generality of its phrasing, not in the particularity of the reason [for its revelation]. Similarly, there is the saying of the Prophet, "Whoever visits me after my death, it is as if they visited me during my life." And there is also the hadith that says, "Whoever visits my grave is guaranteed [wajabat lahu] my intercession."
The following is some of the etiquette that should be observed in the presence of the Messenger of God when visiting his grave: Lowering one's voice, standing with respect [waqar] and humility [khushu'], having the image and the grandeur [haybah] of the Prophet in mind, and to not trespass on the noble grave by touching it, circumambulating around it, or the like. But it is alright to touch his blessed minbar, as was related according to Ahmad. Ibn Qudamah says the following, "It is not preferred to touch or kiss the wall of the Prophet's grave.
Ahmed said, 'I do not know of this.' Al-Athram said, "I saw that the people of knowledge who reside in Madina do not touch the grave of the Prophet, rather they stand to the side and greet him.' Abu ‘Abd Allah said, 'That is what Ibn 'Umar used to do. As for the minbar, it has been related concerning it.' Meaning that Ibrahim ibn 'Abd al-Rahman ibn 'Abd al-Qari' related that he saw Ibn 'Umar put his hand on the Prophet's seat on the minbar and then put it on his face."
Secondly, the ruling of traveling to visit the grave of the Prophet and graves in general: "Shad al-rihal" is a metaphor for traveling, which in itself is not an act of worship, nor is it intended for its own sake when carrying out acts of worship. If one were to say that traveling to visit graves, and traveling to visit the grave of the Prophet is not permissible, then it necessarily follows that the preferability of visiting graves and the preferable nature of visiting the grave of the Prophet only applies to residents of the countries in which they are found. In this case, it would only be permissible for residents of Madina to leave their homes with the intention of visiting the grave of the Prophet, and anyone else who would need to travel in order to do the same thing would be committing a sin. This is not only extremely farfetched, it is wrong, mere false imaginings.
The scholars of juristic methodology have agreed that the means have the same rulings as their ends; so if pilgrimage (haj) is mandatory, then traveling in order to make pilgrimage is mandatory, and if visiting the grave of the Prophet and the graves of the righteous, one's relatives, and the Muslims in general is preferred, then traveling in order to visit them is preferred, for how can the act be preferable and the means of carrying it out forbidden?
The scholars are of the opinion that it is permissible to travel in order to visit graves due to the general evidence [to this effect], and particularly the graves of the prophets and the righteous. As for the hadith, "One does not travel except to three mosques: this mosque of mine, the mosque in Mecca, and the Masjid al-Aqsa," it refers particularly to mosques, so one does not travel except to those three. The evidence for this exclusivity is that it is permissible to travel in order to seek knowledge and for trade.
The scholars have agreed upon this meaning. The following is the explanation of Sheikh Sulayman ibn Mansur who was known as al-Jamal: "'Do not travel,' means in order to pray in them, so it does not oppose traveling for another purpose...al-Nawawi said, 'It means there is no preeminence in traveling to a mosque other than these three,' and he related this according to the majority of scholars. Al-'Iraqi said, 'One of the best interpretations of the hadith is that it only refers to rulings of mosques, so one does not travel to a mosque other than these three. As for seeking other than mosques by traveling for the sake of knowledge, to visit the righteous and one's comrades, for trade, or pleasure, and the like, this is not referred to in the hadith.'
This was made explicit in the narrations of Imam Ahmad and Ibn Abi Shaybah with a good (hasan) chain of transmission according to Abu Sa'id al-Khudari ascribing it to the Prophet, "The one performing prayer should not travel to a mosque in order to pray except the mosque in Mecca, the al-Aqsa mosque, and this, my mosque, and in another narration, "The one praying should not travel..' Al-Subki said, 'There is no place on earth that is preferred above others in and of itself for one to travel to it seeking that preferability except these three cities.' And he said, 'What I mean by preferred is that which the Divine law has taken into consideration and upon which legal rulings have been based. As for other places, one does not travel to them for their own sake, rather [one travels to them] for the sake of making a visit, acquiring knowledge, or the like from among the things that are recommended or permissible.
Some people have been confused by this and they have claimed that traveling to visit other than these three, like Sidi Ahmad al-Badawi and others like him, is included in the prohibition. This is an error because exemption can only be made from the same category (jins) from which they are being exempted. The meaning of the hadith then is that one should not travel to a mosque from among mosques, or a place among places for the sake of that place in and of itself, except to the three mentioned. Traveling in order to make a visit or to acquire knowledge is not traveling to a place, rather it is traveling to the person in the place, so understand.'"
Based on this, traveling in order to visit the grave of the Prophet is recommended because it is the only means to accomplish something that is itself recommended, which is the visit. Similarly, traveling in order to visit the graves of the righteous and one's relatives is recommended, because it is the means [to visit them], and traveling in order to accomplish things that are permissible is permissible.
And God is Most High and Knows best.