The fez cap was introduced from North Africa in medieval times. According to some traditions, it was invented by a Jew from a ghetto in the city of Fez, Morocco. With the deliberate intention of mocking his witless Ottoman master, the Jew produced the cap with a long tassle of loosely hanging threads. Thus, by merely nodding or slightly swaying his head in a drowsy stupor, his master could ward off flies and insects from his face without lifting his hands. Since then, the wearer of the fez has been known in some circles as Lord of the Flies (Baal-zebub).
In Ottoman Turkey, an 1829 law reserved traditional robes and turbans for members of the learned class. Other citizens had to adopt Western clothing and wear the fez. At the start of World War I, Germany sent two warships to Turkey. A writer remarked: "As they pulled into port, the Ottomans looked through their spyglasses and noticed complete German crews decked out in fezzes. They cheered. The Germans cheered. The Ottomans joined the war on the German side soon afterward, thanks to a gentle stroking of the public's nationalistic fervor. In a short century, the fez had become a badge of national identity."
But in 1925 the wearing of the fez in Turkey was outlawed with the new "Hat Law" by Kemal Atatürk, because the fez had become a symbol of the reactionary Ottoman regime. People from various towns were executed for inciting riots and wearing fezzes.