Praise be to Allah.
Firstly, we have already stated in the answer to question no. 20165 that fasting whilst travelling may fall into three categories:
1 – If fasting does not cause hardship, then it is preferable to fast.
2 – If fasting causes hardship, then it is better to break one's fast.
3 – If a person will be harmed by fasting or there is the fear that he may die, then fasting becomes haraam and he has to break his fast.
We have also quoted evidence from the Sunnah concerning that.
Secondly: This hadith to which the questioner refers applies in the third case. If we know the context of the hadith and the reason why it was narrated, that will become clear.
Al-Bukhaari (1946) and Muslim (1115) narrated from Jaabir ibn ‘Abd-Allah (may Allah be pleased with him) that the Messenger of Allah (peace and blessings of Allah be upon him) was on a journey, and he saw a crowd of people and a man who was being shaded. He said, “What is this?” They said, “He is fasting.” He said, “It is not an act of righteousness to fast whilst travelling.”
The words “It is not an act of righteousness …” mean it is not an act of obedience or worship.
What it means is that if it is too difficult for you to fast and you fear that it may harm you. The context of the hadith indicates that this is how it should be interpreted… The hadith has to do with one who may be harmed by fasting.
This is the way in which al-Bukhari understood the hadith. He introduced it by saying:
“Chapter: The words of the Prophet (peace and blessings of Allah be upon him) to one who was being shaded when the heat was too intense, ‘It is not an act of righteousness to fast when travelling.’”
By this introduction he indicated that the reason why the Prophet (peace and blessings of Allah be upon him) said “It is not an act of righteousness to fast when travelling” is the hardship that was mentioned.
Ibn al-Qayyim said in Tahdheeb al-Sunan:
With regard to the words “It is not an act of righteousness to fast when travelling”, this was said concerning a specific person whom the Messenger of Allah (peace and blessings of Allah be upon him) saw being shaded and fasting had exhausted him. So he said these words, i.e., it is not an act of righteousness for a person to exhaust himself to this extent when Allah has allowed him not to fast.
This hadith cannot be interpreted in general terms to mean that it is not an act of righteousness to fast on any type of journey, because it is proven that the Prophet (peace and blessings of Allah be upon him) used to fast when travelling.
Hence al-Khattaabi (may Allah have mercy on him) said:
These words were spoken for a reason, so they are limited to those who are in a similar situation. It is as if he said that it is not an act of righteousness for a traveller to fast if fasting will lead to him suffering in such a manner. And the Prophet (peace and blessings of Allah be upon him) fasted on his journey in the year of the Conquest.
From ‘Awn al-Ma’bood.
And Allah knows best.