The comment about Alexandria is because that was the center of the Jewish community in Egypt in historical times:
(The Gra, based on other sources, corrects “אלכסנדרוס מוקדן” to Trajan.)
Where does that prohibition come from? One of the Rambam’s three sources is from this week’s parasha:
At first glance, this seems like a promise, not a commandment—you will not see Egypt this way, not you shall not. The Rambam gets his interpretation—that it is a commandment—from the Mechilta:
As we know, the Rambam himself, who lists this as a Biblical prohibition, lived most of his adult life in Egypt. The commentators on the משנה תורה address this:
And the Rambam seemed to acknowledge the problem:
In our pasuk, Rashi interprets this as a promise that Egypt will never threaten Israel militarily again:
But if it’s a promise, it didn’t come true.
The Ramban tries to put the two interpretations together and understand the prohibition as a whole:
In the laws of the king, the pasuk says וה׳ אָמַר לכם לא תספון לשוב בדרך הזה עוד, ”Hashem had previously told you not to go back again“; אָמַר is in the past perfect. The only possible reference is to our pasuk, כי אשר ראיתם את מצרים היום לא תספו לראתם עוד. In effect, ספר דברים is a מדרש on ספר שמות. Moshe is saying that while the פשט of our pasuk is a promise, the דרש is that it is a commandment.
So the Mechilta teaches us that this commandment is mentioned three times, and the Rambam understands that there are three subtly different prohibitions. These differences manifested in three eras in history. In שמות, the commandment is not to be impressed by Egypt’s might and military prowess and therefore moving there or even allying with them. This was the sin mentioned in the Mechilta in the days of סנחריב.
The prohibition in דברים יז, the law of the king, is not to be impressed with Egypt’s wealth and culture, as symbolized by their horses. This was the sin of the Jews of Alexandria, who were assimilated into contemporary Egypt.
And the third warning against moving to Egypt? That is part of the תוכחה, the prophecy of the punishments that will come if בני ישראל violate the Torah. It’s not a commandment but a warning: והשיבך ה׳ מצרים באניות בדרך אשר אמרתי לך לא תסיף עוד לראתה. How could the Jews be blamed for that?
The answer is in another pasuk, the prohibition of destroying the altar:
So the prohibition is less “don’t move to Egypt” but “don’t do the sorts of things that will lead inevitably to your exile back into Egypt”. And that was the sin in the days of יוחנן בן קרח when the remnants of the Jewish community ended up in Egypt after the destruction of the first בית המקדש and the murder of גדליה.
So there are three reasons why we end up under the influence of Egypt and societies like her. And in all three cases, ה׳ warns us to maintain our identity and independence, so that we do not end up like Pharoah and his chariots in ים סוף, all washed up.