Rabat
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 starting attacks on shipping.  The pirates did not have to fight 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. The Republic of Bou Regreg did not collapse until 1818.  Even after the republic's collapse, pirates continued to use the port of Rabat. This led to the attack of the city by Austria in 1829 after an Austrian ship had been lost to a pirate attack.