Chefchaouen was founded in 1471 by Moulay Ali Ben Rachid as a mountain fortress to block Portuguese expansion along Morocco's coast, and it soon became a refuge for Muslims and Jews fleeing Andalusia after the fall of Granada. It later became famous for its blue-washed walls, earning it the nickname the "Blue Pearl."