"Doth not even nature itself teach you, that, if a man have long hair, it is a shame unto him?"
- St. Paul, 1 Corinthians 11:14
Although it's not easy to reconcile with the story of Samson, the hair-powered superhero of the Old Testament, nor with the injunction elsewhere in the Bible never to shave the four corners of your hair (Leviticus 19:27, the basis of Rastafarian dreadlocks and Orthodox Jewish peyot - see also Numbers 6:5-6), ever since Saint Paul made his ostensible condemnation of long hair on men it has acted as a handy pretext for the persecution of the long-haired. Actually, the particular word used in the Bible (before it was translated badly from the Greek) apparently refers to fancy hairstyles, the length not being directly referred to; elsewhere (Acts 19:12) a reference to a 'head-band' suggests that Paul himself must have had long hair. However, on the surface Paul's Corinthian hair note looks like such a clear condemnation of long hair on men that it is interpreted this way by various sects to this day.
As early as the Fourth Century BC, Alexander the Great commanded his army to go clean-shaven to avoid giving the enemy anything to grab onto, and since then many armies throughout history have gone short-haired. In the Second Century AD, Pope Anicetus decreed that the clergy should not have long hair, and Saint Patrick renewed this prohibition in 456, expelling a number of longhairs from the church; several subsequent church councils strengthened this official ecclesiastical condemnation. Late in the Eleventh Century, papal decree - enthusiastically surported by the leadership of the church - saw the long-haired male population excommunicated en masse, with orders that they should not be prayed for after death. King Henry I and his court incurred the wrath of the clergy with their longhaired ways; condemnations of lengthy locks on men rang from pulpits up and down the land, with the Archbishop of Canterbury being among the most vocal opponents of the fashion. The church's entreaties were to no avail, though, and the fashion only abated somewhat when a popular and influential knight of the court had a dream in which the devil tried to choke him with his own hair, and on waking found that his mouth was indeed full of hair. He cut his locks off on the spot, and the story got around quickly; the dream was taken as a sign from god, and many, many men cut their hair. Even after this, though, it's said that a year later their hair was long again. Sometimes divine displeasure just isn't enough to sway the tides of fashion.
The single most notable instance of hair-related legislation in history was law enacted by Peter the Great of Russia, beginning in 1705. By this time beards had fallen out of fashion almost everywhere in Europe, but the inhabitants of Russia, scorning the ways of foreigners, clung proudly to theirs. Peter didn't like this. He wanted his men to be clean-shaven, like their fellows on the continent, and the word went out that from a certain date onward all beards were to be shaven, or a tax of one hundred roubles paid for the privilege of keeping them (or, for serfs and priests, one copeck to be paid upon entering or leaving a city). The new tax caused enormous resentment among the populace, but proved a valuable source of income for the Russian government for many years.
In 1838, the king of Bavaria signed an ordinance appearing in German newspapers prohibiting the wearing of moustaches for any reason, with orders that the authorities should arrest and shave any mustachioed party on sight. This command seems to have been singularly successful, with not a single person recorded as having been arrested as a result; moustaches disappeared from public life more or less overnight. Seldom has tonsorial legislation so successfully eliminated its target in the population at large.
Several countries passed laws in the late 20th Century banning long hair on men: Malaysia and Singapore were at one time notorious for their insistence that even short-term visitors must have their hair cut short. The Taliban in Afghanistan, too, banned long hair on men, although beards were encouraged.
It has been quite common in the last few decades for men to be ordered to shave their hair when being sent to prison in Europe and America: One infamous example was the order of the judge in the Oz obscenity trial in the 1960s that the defendants should all have their hair cut off. In 1997, California prison officials banned long hair and beards on inmates on the grounds that they make it too easy for escaped convicts to disguise their identity. Prisoners taken to Camp X-Ray by Americans in the war in Afghanistan have similarly been forcibly shaved. It is widely felt that these forced shavings have little practical value and are performed mainly in order to humiliate the prisoner, which is especially effective in those cases where the hair has been kept long for religious reasons. However, not everybody agrees that the right to stay hairy is a human right worth preserving when it is possible that it might be in some way detrimental to somebody's security, and it doesn't look like the civil libertarians are getting very far on this one.
Main source: Extraordinary Popular Delusions and the Madness of Crowds