Why did Jesus tell Mary Magdalene not to touch him and later permits Thomas to touch him?
The “touch” in the KJV’s “touch me not” (John 20:17) comes from the Greek term haptomai, which carries with it the idea of not only touching someone, but in certain contexts “clinging” to them. Thus, the ESV and NASB translate this verse as Jesus saying to Mary, “Do not cling to me” or “Stop clinging to me.”
Later, when he would greet the other women who had come to the tomb that morning, they would respond by “(taking) hold of his feet” (Matt. 28:9); in other words, they clung to him, likely overcome with joy at seeing him alive again and clinging to his feet in a way that likely showed their desire to not lose him again. Mary likely responded in the same way. Thus, Jesus responded by requesting that she not cling to him because “I have not yet ascended to the Father.” In other words, “Mary, you don’t have to hold on to me as if I’m about to leave you forever. It’s going to be a few weeks before I ascend into heaven, so you’re going to see me again.” It’s also possible that he was gently informing her that no amount of clinging to him would prevent him from ascending to his Father in heaven when the time came.
Later, when he showed himself to Thomas (who had previously doubted the other apostles’ story of his resurrection), he invited Thomas to touch the wounds in his hands and sides as a way for him to use his sense of sight and touch to verify that Jesus had in fact come back to life (John 20:24-27). The indication from verse 27 is that Jesus was inviting Thomas to touch the wounds in his hands and “put out (his) hand, and place it in (Jesus’) side,” something different from the clinging Mary had been doing. It was a different kind of touching, for different purposes, and done in different contexts.