Actions are but by intention and every man shall have but that which he intended. Thus he whose migration (Hijrah) was for Allah and His Messenger, his migration migration was for Allah and His Messenger , and he whose migration was to achieve some worldly benefit or to take some woman in marriage, his migration was for that which he migrated.
It is related by the two imaams of the scholars of Hadith, Aboo `Abdullah Muhammad ibn Ismaa`eel ibn Ibraheem ibn al-Mugheera ibn Bardizbah al-Bukhaaree and Aboo-l-Husain Muslim ibn al-Hajjaaj ibn Muslim al-Qushairee an-Naisaabooree, in their two Saheehs, which are the soundest of compiled books.
Explanation of Hadeeth # 01
Everything we do has to be intended to be for Allah, i.e., for anything we do we want to make sure that it will be acceptable to Him and will not in any way interfere with the upholding of His Deen. A good action with the wrong intention will not get us any reward in the Hereafter.