Question
What is the Sharee‘ah ruling on a person who goes to Madeenah under the pretext of performing prayer in the Masjid (Mosque) of the Prophet when he actually intends to visit his grave?
Answer
All perfect praise be to Allah, the Lord of the worlds. I testify that there is none worthy of worship except Allah, and that Muhammad is His Slave and Messenger.
It is permissible for a person who is near to Madeenah and can reach it without travel to have the intention of visiting the grave of the Prophet even if he solely went for that purpose. As for those who travel to Madeenah, it is permissible for one to visit the grave of the Prophet if he goes with the intention of performing prayer in the Masjid. But if he travels with the sole intention of visiting the grave, this is not approved under Sharee‘ah according to most scholars. Nevertheless, this is not expected to be done by those who know the virtue of performing prayer in the Masjid of the Prophet .
Shaykh-ul-Islam, Ibn Taymiyyah said
A person who is knowledgeable about the Sharee‘ah would not do that. He is fully aware that the Messenger of Allah made it desirable for Muslims to travel to his Masjid and perform the prayer there. So, how is it that such person does not make praying in the Masjid his basic objective while he is going there in all cases. Intention is absent only in case of ignorance. A person may be ignorant that traveling to the Masjid of the Prophet is desirable because of the Masjid itself, not the grave; or the traveler may be unaware that he will reach the Masjid. When a person is knowledgeable about the two matters, it necessarily follows that he intends traveling to the Masjid to pray there in the first place.
Allah Knows best.