Gezer lies in ancient "no-man's-land" - in sight of the hill country of Israel and in sight of the Philistine coast. Both wanted it, but its safe to say that neither had it...at least not for long. During the time of David it was apparently under Canaanite rule, and though he managed to expand his kingdom greatly, he didn't make it quite that far.
Why was Gezer such a sought after city, the proverbial belle of the ball (locally speaking? Simply put: trade. Gezer was along the ancient international highway. Traders and armies regularly passed this town: Egypt, going to fight Syria (and vice-versa); spices, fresh off the windy desert, bound for Tyre; so on an so forth. This was a route that could enrich whatever kingdom controlled it. Control over even one city on the route would have been a huge boost for relatively small kingdoms like Judah and Philistia. However, its great value also entailed great risk: there was always someone with a bigger army, ready to snatch it away. In the game of Risk, it would be Europe: you bide your time, stacking up armies and then fight hard to take it all over in one turn, only to be utterly annihilated by your opponents in the next round.
There are, however, a large number of towns along this route, of roughly equal strategic importance...except to the kingdom of Israel. The easiest routes from the coast to the capital city of Jerusalem is through the Aijalon valley, and the mouth of the Aijalon valley was guarded by Gezer. Safety, as well as riches, were on the line for the kingdom of Judah.
Eventually David died and Solomon, his son, became king. Solomon was not a man of war and would have no hope of capturing Gezer on his own. He used other methods of expanding his kingdom, most of them apparently involving marriage. The Bible records that he had 700 wives. Most of them were princesses of a sort, married to validate his control over a region or to seal a covenant with a neighboring kingdom and effectively widening Solomon's range of influence.
One such marriage just happened to be to a daughter from a very powerful empire: Egypt. The fact that such a marriage occurred gives us reason to believe that at this time either Solomon was very powerful indeed, or Egypt was very weakened, or possibly a combination of both. Very powerful kings did not give their daughters in marriage to very weak kings, from whom no help could be expected. Thus, at this time at least, it seems that Israel and Egypt were on roughly equal footing. For her dowry, the king of Egypt conquered Gezer and gave it to his daughter.
And this is the account of the forced labor that King Solomon drafted to build the house of the LORD and his own house and the Millo and the wall of Jerusalem and Hazor and Megiddo and Gezer. (Pharaoh king of Egypt had gone up and captured Gezer and burned it with fire, and had killed the Canaanites who lived in the city, and had given it as dowry to his daughter, Solomon's wife; so Solomon rebuilt Gezer).
Archaeologically, Hazor, Megiddo, and Gezer all have something in common: they all have 6-chambered gates, which seem to date to about the time of Solomon (thus, they are called, "Solomonic gates" - original, no?).
So, through this alliance, Solomon gained Gezer. This gave him relative safety from attack from the east, and probably also increase his income from taxes immensely. Egypt, in return, was likely allowed safe passage through the area, and possibly given a break from the tolls. Everything seemed happy.
Then Solomon died. His son Rehoboam became king in his place, but he was not the son of the Egyptian princess. Things had changed.
1 Kings 14:25-26 tells the story:
In the fifth year of King Rehoboam, Shishak king of Egypt came up against Jerusalem.He took away the treasures of the house of the LORD and the treasures of the king's house. He took away everything. He also took away all the shields of gold that Solomon had made.
What the story doesn't tell us is something that is obvious with a quick glance at the geography. Shishak would have come up from Egypt travelling along the coast. To get to Jerusalem from the coast he would have gone right past Gezer, the place his father conquered and gave to Solomon. He must have reclaimed it in the name of Egypt and kept right on a-trucking up to Jerusalem. Who knows if there was even a fight? Technically, the city belonged to Pharaoh's daughter as her dowry. Perhaps there was even a small Egyptian outpost there who just opened those big "Solomonic Gates" and let the rest of the army in to refuel before climbing up to Jerusalem. Maybe that was the plan all along.
It's clever. Sneaky. And in a place where Jacob is a hero for tricking Esau and Laban, its very Middle Eastern. It is good to be friendly, to form alliances...but everyone is always looking out for their own interests. If friendship will serve that, then friendship it will be. Until something more profitable comes along. It doesn't sit well with our western mindset, this trickery and two-facedness, but it fits with this land, both now and then.