Here is a bit of history of Rabat from Wikipedia:
Rabat’s history began with a settlement, known as Chellah on the banks of the Oued Bou Regreg in the third century BC. In 40 AD, Romans took over Chellah and converted it to the Roman settlement of Sala Colonia. Rome held the colony until 250 AD when they abandoned it to Berber rulers. In 1146, the Almohad ruler Abd al-Mu’min turned Rabat’s ribat into a full scale fortress to use as a launching point for attacks on Spain. In 1170, due to its military importance, Rabat acquired the title Ribatu l-Fath, meaning “stronghold of victory,” from which it derives its current name.
Yaqub al-Mansur (known as Moulay Yacoub in Morocco), another Almohad Caliph, moved the capital of his empire to Rabat. He built Rabat’s city walls, the Kasbah of the Udayas and began construction on what would have been the world’s largest mosque. However, Yaqub died and construction stopped. The ruins of the unfinished mosque, along with the Hassan Tower, still stand today.
Yaqub’s death initiated a period of decline. The Almohad empire lost control of its possessions in Spain and much of its African territory, eventually leading to its total collapse. In the 13th century, much of Rabat’s economic power shifted to Fez. In 1515 a Moorish explorer, El Wassan, reported that Rabat had declined so much that only 100 inhabited houses remained. An influx of Moriscos, who had been expelled from Spain, in the early 17th century helped boost Rabat’s growth (principal families: Mouline [Molina], Bargach [Vargas], Balafrej [Palafresa], Moreno, Baena, Olivares [Loubaris],…).
Rabat and neighboring SalÃ© united to form the Republic of Bou Regreg in 1627. The republic was run by Barbary pirates who used the two cities as base ports for launching attacks on shipping. The pirates did not have to contend with any central authority until the Alaouite Dynasty united Morocco in 1666. They attempted to establish control over the pirates, but failed. European and Muslims authorities continued to attempt to control the pirates over many years, but the Republic of Bou Regreg did not collapse until 1818. Even after the republic’s collapse, pirates continued to use the port of Rabat, which led to the shelling of the city by Austria in 1829 after an Austrian ship had been lost to a pirate attack.
The French invaded Morocco in 1912 and established a protectorate. The French administrator of Morocco, General Hubert Lyautey, decided to relocate the country’s capital from Fez to Rabat. Among other factors, rebellious Berbers (native Moroccans) had made Fez an unstable place. Sultan Moulay Youssef followed the decision of the French and moved his residence to Rabat. In 1913, Gen. Lyautey hired Henri Prost who designed the Ville Nouvelle (Rabat’s modern quarter) as an administrative sector. When Morocco achieved independence in 1956, Mohammed V, the then King of Morocco, chose to have the capital remain at Rabat.