During the Middle Ages, Milan prospered as a centre of trade due to its command of the rich plain of the Po and routes from Italy across the Alps. The war of conquest by Frederick I Barbarossa against the Lombard cities brought the destruction of much of Milan in 1162. After the founding of the Lombard League in 1167, Milan took the leading role in this alliance. As a result of the independence that the Lombard cities gained in the Peace of Constance in 1183, Milan became a duchy. In 1208 Rambertino Buvalelli served a term as podestŕ of the city, in 1242 Luca Grimaldi, and in 1282 Luchetto Gattilusio. The position could be fraught with personal dangers in the violent political life of the medieval commune: in 1252 Milanese heretics assassinated the Church's Inquisitor, later known as Saint Peter Martyr, at a ford in the nearby contado; the killers bribed their way to freedom, and in the ensuing riot the podestŕ was very nearly lynched. In 1256 the archbishop and leading nobles were expelled from the city. In 1259 Martino della Torre was elected Capitano del Popolo by members of the guilds; he took the city by force, expelled his enemies, and ruled by dictatorial powers, paving streets, digging canals, successfully taxing the countryside. His policy, however, brought the Milanese treasure to collapse; the use of often reckless mercenary units further angered the population, granting an increasing support for the Della Torre's traditional enemies, the Visconti.
The Castello Sforzesco, symbol of the power of the House of Sforza.On 22 July 1262 Ottone Visconti was created archbishop of Milan by Pope Urban IV, against the Della Torre candidate, Raimondo della Torre, Bishop of Como. The latter thus started to publicize a allegations of the Visconti's nearness to the heretic Cathars and charged them of high treason: the Visconti, who accused the Della Torre of the same crimes, were then banned from Milan and their properties confiscated. The civil war which ensued caused more damage to Milan's population and economy, lasting for more than a decade. Ottone Visconti led a group of exiles unsuccessfully against the city in 1263, but after years of escalating violence on all sides, finally, after the victory in the Battle of Desio (1277), he won the city for his family. The Visconti succeeded in ousting the della Torre forever, ruling the city and its possession until the 15th century.
Much of the prior history of Milan was the tale of the struggle between two political factions-the Guelphs and the Ghibellines. Most of the time the Guelphs were successful in the city of Milan. However, the Visconti family was were able to seize power (signoria) in Milan, based on their "Ghibelline" friendship with the German Emperors In 1395, one of these emperors, Wenceslas (1378-1400), raised the Milanese to the dignity of a duchy. Also in 1395, Gian Galeazzo Visconti became duke of Milan. The Ghibelline Visconti family was to retain power in Milan for a century and a half from the early 14th century until the middle of the 15th century.
In 1447 Filippo Maria Visconti, Duke of Milan, died without a male heir; following the end of the Visconti line, the Ambrosian Republic was enacted. The Ambrosian Republic took its name from St. Ambrose, popular patron saint of the city of Milan. Both the Guelph and the Ghibelline factions worked together to bring about the Ambrosian Republic in Milan. However, the Republic collapsed when in 1450, Milan was conquered by Francesco Sforza, of the House of Sforza, which made Milan one of the leading cities of the Italian Renaissance.