حَدَّثَنَا عَبْدُ اللَّهِ بْنُ مَسْلَمَةَ ، قَالَ : أَخْبَرَنَا مَالِكٌ ، عَنْ يَحْيَى بْنِ سَعِيدٍ ، عَنْ مُحَمَّدِ بْنِ إِبْرَاهِيمَ ، عَنْ عَلْقَمَةَ بْنِ وَقَّاصٍ ، عَنْ عُمَرَ ، أَنَّ رَسُولَ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ ، قَالَ : الْأَعْمَالُ بِالنِّيَّةِ وَلِكُلِّ امْرِئٍ مَا نَوَى ، فَمَنْ كَانَتْ هِجْرَتُهُ إِلَى اللَّهِ وَرَسُولِهِ فَهِجْرَتُهُ إِلَى اللَّهِ وَرَسُولِهِ ، وَمَنْ كَانَتْ هِجْرَتُهُ لدُنْيَا يُصِيبُهَا أَوِ امْرَأَةٍ يَتَزَوَّجُهَا فَهِجْرَتُهُ إِلَى مَا هَاجَرَ إِلَيْهِ .
The Holy Prophet (PBUH) said, "Actions are justified by the intention (or they are rewarded according to the intention) and every person will get what he intends." So, whoever migrates for the pleasure of Allah and His Messenger, his migration will be towards Allah and His Messenger, and whoever migrates to earn money or to marry a woman, then his migration will be to them. Will be for work.
Sahih Bukhari
Hadeeth No #54