In 1235, Danylo Romanovych of Halych granted the town a city charter and moved the capital of his domain in 1241–1272 after destruction of Halych by Mongols in 1240–1241. Danylo also built a new castle atop the hill in 1237, one of the few Ruthenian castles that withstood Mongol attacks, and established an Orthodox eparchy (diocese) centered at the Basilica of the Birth of the Virgin Mary. Until the 14th century, the town developed as part of Kingdom of Galicia–Volhynia and then as part of the short-lived Princedom of Chełm and Belz (see Duchy of Belz). In 1366, king Casimir III the Great of Poland took control of the region after his victory in the Galicia–Volhynia Wars. On 4 January 1392, the town was relocated and granted rights under Magdeburg Law, with vast internal autonomy and the town saw an influx of Polish and other Catholic settlers.