Four distinct nations
First, it's important to understand that each of the four nations (other than Israel) mentioned here is distinct from the others, though they did have various interrelationships. Here are their origins in the Hebrew Table of Nations, and their territories at the time of the conquest of the Holy Land.
It is surprisingly difficult to find a map online that shows the territories of all four of these nations in a single view. Here is a 1748 map by Emanuel Bowen (source) that gives the general idea of where they were located geographically at the time of the conquest:
(If anyone can point me to a more contemporary freely available map that covers all four of these nations, please do so. Thanks!)
The Amorites were descendants of Ham through his son Canaan:
The sons of Ham: Cush, Egypt, Put and Canaan. (Genesis 10:6)
Canaan was the father of Sidon his firstborn, and of the Hittites,
Jebusites, Amorites, Girgashites, Hivites, Arkites, Sinites,
Arvadites, Zemarites and Hamathites. (Genesis 10:15-18, italics added)
During their history they had wide-ranging territories both within the land of Canaan (Palestine) and in the Transjordan area, and even projected themselves across the Fertile Crescent into Babylonia. At the time of the Israelite conquest of the Holy Land, they occupied parts of the southern hill country of Canaan, and the area east of Jordan that had been the northern part of Moab's territory and the western part of Ammon's territory.
The Amorites had earlier pushed the Ammonites east from the Jordan, and had pushed the Moabites out of the northern part of their territory at the southern end of the Jordan, so that in addition to the hill country of Judea in southern Canaan proper, they also occupied a swath of land on the other side of the Jordan that separated the Ammonites from the Jordan and the Ammonites and Moabites from each other.
Since they were Canaanites, the Amorites were considered mortal enemies of the Israelites.
The Midianites were descendants of Abraham through his wife Ketura:
Abraham had taken another wife, whose name was Keturah. She bore him
Zimran, Jokshan, Medan, Midian, Ishbak and Shuah. Jokshan was the
father of Sheba and Dedan; the descendants of Dedan were the
Ashurites, the Letushites and the Leummites. The sons of Midian were
Ephah, Epher, Hanok, Abida and Eldaah. All these were descendants of
Keturah. (Genesis 25:1-4, italics added)
The Midianites lived in the territory south of Moab and Edom, stretching down into the area east of the Gulf of Aqaba, which is the eastern finger of water running northward from the Red Sea.
Moses' wife Zipporah was a Midianite (see Exodus 2:15-22), and Moses had friendly relations with his Midianite family after the Exodus (see Exodus 18; Numbers 10:29-33).
However, starting with Numbers 22:1-7, relations between Israel and Midian deteriorated.
The Moabites and Ammonites
The Moabites and Ammonites were descendants of the two sons of Lot, Abraham's nephew. See Genesis 19:30-38.
The Moabites lived in the area east of the Dead Sea, south of the Ammonites. Their territory had earlier extended north to the southern end of the land east of the Jordan, but had been pushed out of the northern part of their territory by the Amorites.
The Ammonites lived in the area east of the Jordan River, north of the Moabites. However, by the time of the conquest they had been pushed farther east, away from the Jordan. The Amorite territory also separated the Ammonites from their kinsmen, the Moabites.
Presumably because they were relatives of Abraham, Israel was not allowed to attack Moab or Ammon during the initial conquest of the Holy Land (see Deuteronomy 2:9, 18-19, 37).
However, during the period of the Judges, and right through to the period of united kingdom of Israel and the divided kingdoms of Judah and Israel, there was frequent conflict between the Israelites and the Moabites and Ammonites.
Now to address the question:
Why are the Ammonites excluded instead of the Midianites in Nehemiah
Nehemiah 13:1-3 refers to something "found written in the Book of Moses." This is a reference to this passage from the book of Deuteronomy:
No Ammonite or Moabite or any of their descendants may enter the
assembly of the Lord, not even in the tenth generation. For they did
not come to meet you with bread and water on your way when you came
out of Egypt, and they hired Balaam son of Beor from Pethor in Aram
Naharaim to pronounce a curse on you. However, the Lord your God would
not listen to Balaam but turned the curse into a blessing for you,
because the Lord your God loves you. Do not seek a treaty of
friendship with them as long as you live. (Deuteronomy 23:3-6)
Notice that there are two reasons given for the exclusion:
- "They did not come to meet you with bread and water on your way when you came out of Egypt."
- "They hired Balaam son of Beor from Pethor in Aram Naharaim to pronounce a curse on you."
Hiring Balaam to curse the Israelites
There is no record, aside from Deuteronomy 23:3-6 and the reference to it in Nehemiah 13:1-3, of the Ammonites being a party to the hiring of Balaam. As quoted from Numbers 22:2-5 in the question, it was the Moabites and Midianites who hired Balaam to pronounce a curse on Israel. And the Midianites are definitely not the same as the Ammonites. So the second point applies primarily to the Moabites.
However, it is possible that the Ammonites, who were the close kin of the Moabites, were considered guilty by association.
This is the most likely reason that the Ammonites are excluded instead of the Midianites in Deuteronomy 23:3-6. The Ammonites are closely associated with the Moabites, since they are descended from two brothers.
The Midianites are a more distant foe geographically.
The Midianites are also slightly closer kinsmen to the Israelites, being descendants of Abraham, as compared to the Moabites and Ammonites, whose nearest common ancestor with the Israelites was Abraham's father Terah. This closer kinship with the Midianites might also have contributed to the substitution of the Ammonites for the Midianites in the exclusion from the assembly. See, for example, the first half very next verse in Deuteronomy:
Do not despise an Edomite, for the Edomites are related to you.
The Edomites were the descendants of Esau, Jacob's brother, and thus were the closest relatives of the Israelites--who were descended from Jacob.
Not meeting the Israelites with bread and water
There is conflicting testimony about whether the Israelites actually passed through Moabite territory on their way to the conquest of the Holy Land. It appears that they at least passed through the edges of Moabite territory:
Then the Israelites traveled to the plains of Moab and camped along
the Jordan across from Jericho. (Numbers 22:1)
The plains of Moab were at the northern end of Moab's original territory, east of the southern end of the Jordan and the northern end of the Dead Sea. This territory had earlier been taken from them by the Amorites--whom the Israelites had now defeated. This is the scene of Balaam's three attempts to curse Israel in Numbers 22-24. Technically this was not Moabite territory at that time, but it had historically been Moabite territory.
The events of Numbers 25, in which the Israelite men get involved with Moabite women and their gods, bringing a plague upon themselves, also seems to take place in, or close to, Moabite territory.
And Deuteronomy 2:8-23 tells of the segment of the Israelite's travel "along the desert road of Moab" (Deuteronomy 2:8).
Similarly, the Israelites seem not to have passed through the territory of the Ammonites, though there is conflicting testimony about this as well.
This conflicting testimony is highlighted in the story of Jephthah in Judges 10:6-12:7. Specifically:
Then Jephthah sent messengers to the Ammonite king with the question:
"What do you have against me that you have attacked my country?"
The king of the Ammonites answered Jephthah’s messengers, "When Israel
came up out of Egypt, they took away my land from the Arnon to the
Jabbok, all the way to the Jordan. Now give it back peaceably."
Jephthah sent back messengers to the Ammonite king, saying:
"This is what Jephthah says: Israel did not take the land of Moab or
the land of the Ammonites. But when they came up out of Egypt,
Israel went through the wilderness to the Red Sea and on to Kadesh.
Then Israel sent messengers to the king of Edom, saying, 'Give us
permission to go through your country,' but the king of Edom would not
listen. They sent also to the king of Moab, and he refused. So Israel
stayed at Kadesh.
"Next they traveled through the wilderness, skirted the lands of Edom
and Moab, passed along the eastern side of the country of Moab, and
camped on the other side of the Arnon. They did not enter the
territory of Moab, for the Arnon was its border." (Judges 11:12-18)
Here the Ammonites claim that the Israelites not only entered their territory, but seized it.
Jephthah, however, denies that the Israelites took any Moabite or Ammonite territory, and says that the Israelites went around the eastern edge of the territory of Moab. (He is silent on whether the Israelites passed through Ammonite territory.)
The Ammonites seem to have been attempting to get the Israelites to give back the land east of the Jordan that the Amorites had taken from them earlier, which the Israelites then took from the Amorites. Apparently the Ammonites still considered it their land.
This conflicting testimony is also reflected in the description of the tribal allotment of Gad:
This is what Moses had given to the tribe of Gad, according to its
The territory of Jazer, all the towns of Gilead and half the Ammonite
country as far as Aroer, near Rabbah; and from Heshbon to Ramath
Mizpah and Betonim, and from Mahanaim to the territory of Debir; and
in the valley, Beth Haram, Beth Nimrah, Sukkoth and Zaphon with the
rest of the realm of Sihon king of Heshbon (the east side of the
Jordan, the territory up to the end of the Sea of Galilee). These
towns and their villages were the inheritance of the Gadites,
according to their clans. (Joshua 13:24-28, italics added)
Whether or not the Israelites actually transited any Moabite or Ammonite land, the first charge against the Moabites and Ammonites was a lack of hospitality, expressed in the words Moses addressed to the Israelites: "They did not come to meet you with bread and water on your way" (Deuteronomy 23:4).
As (somewhat distant) kinsmen of the Israelites through Abraham's nephew Lot, the Israelites could reasonably have expected that the Moabites and Ammonites would be hospitable to them as they passed through their territory, based on the ancient Middle Eastern code of hospitality.
Instead, the Moabites forced the Israelites to skirt their territory, attempted to bring a curse down upon them through hiring Balaam, and (through their women) seduced them into committing adultery and worshiping other gods besides the Lord.
The Ammonites also refused entry into their land, as suggested in various passages quoted above, and in this passage:
But Sihon would not let Israel pass through his territory. He mustered
his entire army and marched out into the wilderness against Israel.
When he reached Jahaz, he fought with Israel. Israel, however, put him
to the sword and took over his land from the Arnon to the Jabbok, but
only as far as the Ammonites, because their border was fortified.
(Numbers 21:23-24, italics added)
Clearly although the Israelites approached the border of Ammonite territory, they were not allowed entry. The "fortified border" was closed to them, and blocked them from entering or passing through.
- Neither the Moabites or the Ammonites were hospitable to the
Israelites as they passed through, even though they were distant
kinsmen. Instead, they blocked the Israelites, opposed
them, and caused them problems.
- Though the Ammonites are not mentioned in Numbers 22 as a party to
the hiring of Balaam to curse the Israelites, they may have been
considered guilty by association, given that they were close kinsmen
of the Moabites.
There are many passages throughout the Old Testament recounting a long history of hostility by the Moabites and Ammonites against Israel.
The exclusion of the Moabites and Ammonites from the assembly of the Lord was a result of their lack of hospitality, and even outright hostility, against Israel when the Israelites passed through their territories. This was contrary to the law of hospitality that should have been in effect for their distant kinsmen.
The Midianites were closer kin to the Israelites, and were also initially less hostile to them. This is reflected in the friendly relations between Moses and his Midianite family after the Exodus. However, hostilities did flare up between the two nations, as recorded in the books of Numbers, Joshua, and Judges. These battles seem to have decimated the Midianites to the point that they faded away as a serious enemy.
In contrast, the Moabites and Ammonites continued to be recurring enemies of Israel throughout its history, and there are many diatribes against them in the Prophets.
Deuteronomy and Nehemiah reflect this greater antipathy against the Moabites and Ammonites as compared to the Midianites, as well as the fading away of Midian as a serious enemy early on in the history of the nation of Israel.