عن أميرِ المؤمنينَ [أبي حفصٍ عمرَ بنِ الخطَّابِ] رَضِي اللهُ عَنْهُ قالَ: سَمِعْتُ رسولَ اللهِ صَلَّى اللهُ عَلَيْهِ وَسَلَّمَ يقولُ: “إِنَّمَا الأَعْمَالُ بالنِّيَّاتِ وإِنَّمَا لِكُلِّ امْرِئٍ ما نَوَى، فَمَنْ كَانَتْ هِجْرَتُهُ إِلَى اللهِ وَرَسُولِهِ فَهِجْرَتُهُ إِلى اللهِ وَرَسُولِهِ، وَمَنْ كَانَتْ هِجْرَتُهُ لِدُنْيَا يُصِيبُهَا أَو امْرَأَةٍ يَنْكِحُهَا فَهِجْرَتُهُ إِلَى مَا هَاجَرَ إِلَيْهِ”. رواه إماما المحدِّثينَ: أبو عبدِ اللهِ محمَّدُ بنُ إسماعيلَ بنِ إبراهيمَ بنِ المغيرةِ ابنِ بَرْدِزْبَه البُخاريُّ. وأبو الحُسَيْنِ مسلِمُ بنُ الحجَّاجِ بنِ مُسلمٍ القُشيْريُّ النَّيْسَابوريُّ في صحيحيهما اللَّذين هما أصحُّ الكتُبِ الْمُصَنَّفةِ.
From the Chief of the Believers, Abī Hafṣ ʿUmar bin al-Khaṭṭāb (raḍiyallāhu anhu) who said: I heard the Messenger of Allaah (ṣallallāhu ʿalayhi wasallam) saying:

“Verily actions are based upon the intentions (behind them) and every man shall have (in accordance) with what he intended. Thus, he whose migration was (for the sake of) Allāh and His Messenger, then his migration was indeed for Allāh and His Messenger. And he whose migration was to attain a portion of the world or to take some woman in marriage, then his migration was for that for which he migrated.”

It was related by the two Imāms of the Muḥaddithīn, Abū ʿAbdillāh Muḥammad bin Ismāʿīl bin ʾIbrāhīm bin al-Mughīrah ibn Bardizbah al-Bukhārī and Abū al-Ḥusayn Muslim bin al-Ḥajjāj bin Muslim al-Qushayrī al-Naysabūrī in their two Ṣaḥīḥ compilations which are the most authentic of compiled books.

Related by Muslim, al-Bukhārī