“Now there was a famine in the land, besides the previous famine that had occurred in the days of Abraham. So Isaac went to Gerar, to Abimelech king of the Philistines. The LORD appeared to him and said, “Do not go down to Egypt; stay in the land of which I shall tell you.” Gen. 26:1-2
The famine and the encounter with Abimelech are just the first two of many parallels between the Isaac and Abraham narratives. And just as with Abraham, Isaac was going to head to Egypt due to the famine, by way of Gerar.
We may make the assumption that God did not want Isaac to go to Egypt due to what transpired between Abraham and Pharaoh regarding Sarah, that perhaps the people of Egypt are more wicked than Gerar. However The second time that Abraham was not honest about Sarah being his wife, the same thing happened here, in Gerar, with king Abimelech, in very similar fashion.
Despite this, it appears God was alright with Isaac going to Gerar. Gerar is still within the land of Canaan, the area that would become known as the Promised Land.
Previously in Genesis 24, Abraham calls upon his oldest servant to go and find a wife for Isaac. The servant then asks “Suppose the woman is not willing to follow me to this land; should I take your son back to the land from where you came?” (Gen. 24:5)
Abraham’s response explains much:
Then Abraham said to him, “Beware that you do not take my son back there! “The LORD, the God of heaven, who took me from my father’s house and from the land of my birth, and who spoke to me and who swore to me, saying, ‘To your descendants I will give this land,’ He will send His angel before you, and you will take a wife for my son from there. “But if the woman is not willing to follow you, then you will be free from this my oath; only do not take my son back there.” (Gen. 24:6-8)
Abraham was so adamant about Isaac not going, that he would rather his son be without a wife, despite having a great promise from God concerning the number of his descendants. This appears to be less about preventing Isaac from going to Egypt and more about Isaac staying in Canaan. Abraham’s point is that his journey led up to this: he is now in Canaan, the land promised to him and his descendants; thus he wants Isaac to establish himself here thoroughly, and does not want him to leave.
The Midrash (Bereishit Rabba 64:3) explains that an offering to God must stay in the temple courtyard, and thus, because Isaac was an offering to God, he must stay within the bounds of the Promised Land.