King Hassan II, who ruled Morocco for 38 years, acted as a go-between in Egyptian-Israeli efforts to make peace and prolonged the life of his 300-year-old dynasty in an era when monarchies in Libya, Egypt, Iraq and Iran fell to socialist revolutions or the force of militant Islam, died yesterday in Rabat. He was 70. The cause of death was a heart attack, Crown Prince Sidi Mohammed, the King's eldest son and successor, announced on state television. The King, who had been in fragile health since he was hospitalized in the United States four years ago for lung problems, had been admitted earlier in the day to the Avicenne hospital in Rabat, the capital, with an acute lung infection, according to a statement by the palace. Moroccan television said the funeral would be Sunday. The White House said that President Clinton planned to attend. As King, Sidi Mohammed is expected to continue his father's policies, including close ties with Washington and active pursuit of peace in the Middle East. The United States and its allies considered King Hassan one of the most Western-oriented of Arab leaders, a ruler who outmaneuvered Islamic militants in his country and stood out among his peers for his openness to rapprochement with Israel. Through the years he acted as an intermediary in Middle East diplomacy, helping to arrange a visit to Jerusalem in 1977 by Egypt's President, Anwar el-Sadat, and during the 1980's, meeting with the Israeli leaders Yitzhak Rabin and Shimon Peres when other Arabs shunned them. A master at managing Morocco's complex quilt of ethnic and ideological forces, he maintained a hold on power that was by turns iron-fisted and deftly offhand. He survived half a dozen assassination attempts and uprisings. On one occasion, he intimidated the leader of rebel troops by looking him in the eye and reciting the first verse of the Koran. Another time, when pilots of his air force attacked his Boeing 727 jetliner, the King, himself a pilot, seized the radio and shouted, ''Stop firing! The tyrant is dead!'' -- fooling the rebels into breaking off their attack. The heir to the Alawite dynasty, which claimed direct descent from the Prophet Mohammed and ruled the Sharifian empire of the Western Sahara, Hassan II was the author of Morocco's first Constitution. But he was at heart an autocrat, and democracy waxed and waned at his pleasure. He tolerated opposition parties and a relatively free press that could offer opinions on policy matters. But criticism of the monarchy was forbidden, and his ruthlessness in crushing opponents was criticized by human rights groups. Economic and political reform proceeded steadily through his years in power. Though the pace was slower than his critics would have liked, said William Zartman, director of African Studies at Johns Hopkins University in Baltimore, many would agree that the country was better off during the final years of his reign than it was when he came to power. His success lay in an ability to be different things to different people. He kept Morocco's elite content with royal patronage and instituted market-oriented reforms that improved the lives of the urban middle class. He used his position as ''Commander of the Faithful'' to woo the rural peasantry, quadruple the number of mosques and build the world's largest, the Great Mosque of Hassan the II. Completed in 1993, the 54-acre complex was built on the edge of the sea near Casablanca, with a tower more than 650 feet tall and equipped with a laser that beamed at night toward Mecca. ''He had deep understanding from the early days of the tribal mentality of Morocco and the importance of the throne as a unifying force,'' said Robert H. Pelletreau, a former Assistant Secretary of State for Near Eastern Affairs, who knew the King well. ''He was a superb student, and he could be exceedingly charming.'' Moroccans said of King Hassan that he had ''baraka,'' or ''blessedness,'' an Arabic expression for a charismatic person blessed with divine protection. Yet when he ascended the throne on Feb. 26, 1961, most observers expected him to fail. Moulay Hassan ben Mohammed Alaoui was born on July 9, 1929, the oldest of six children of Sultan Sidi Mohammed ben Youssef. Most of Morocco was then a protectorate of France, except for sections governed by Spain in the northwest and southern coast and the city of Tangier, an international zone. As World War II unfolded, resistance to colonial rule grew. After the fall of France, the Free French forces promised independence if Morocco would cooperate in the war against the Axis, a promise that Paris proved unwilling to keep. After the war, tension rose between the Sultan and the French, but the young Prince Moulay Hassan was educated as befitted the heir of two traditions: He attended the imperial college at Rabat, where instruction was in Arabic and French. Later he earned a law degree from the University of Bordeaux and served in the French Navy aboard the battleship Jeanne d'Arc. But his father's agitation for Moroccan self-government continued, and in 1953 the French forced the Sultan into exile. In 1954 and 1955, as rioting and guerrilla warfare increased, Prince Moulay's father regained his title, and the following year, Morocco won independence. Prince Moulay worked with his father, now Mohammed V, to maintain the monarchy's authority during a time of social discontent and the conflicting expectations of those who fought for Moroccan independence. In 1957, he became the commander in chief of the Royal Moroccan Army, which was splintered between officers who had been loyal to the French and the former rebels. Prince Moulay kept the military occupied with civilian projects, and led it to victory against rebel Berber tribesmen in the Rif mountains in 1958. But in the shantytowns of Rabat, Casablanca and other cities, opposition simmered against the royal house. Though the monarchy looked to Paris and Washington for financial support, it needed to placate the leftist opposition. Declaring neutrality in the cold war, the Prince made overtures to Moscow and accepted Soviet military aid. ''As an Islamic people,'' he told The Associated Press in 1961, ''we have the right to practice bigamy. We can wed East and West and be faithful to both.'' Such comments caused consternation in Western capitals. So did the reputation of the young Prince, whom the Western press often portrayed as a playboy who liked gambling and actresses and was overly concerned with his wardrobe. After Mohammed V died of heart failure following what was expected to be a minor operation, Prince Moulay, who had been named Prime Minister in 1960, moved quickly to establish his rule. His Constitution, which was ratified in 1962, guaranteed freedom of the press and of religion, and created an elected legislature. But the new Parliament, fractured by bitter rivalries, proved ineffectual. The new King retained the power to name the Prime Minister, disband the legislature and control the army. In the mid-1960's, student agitation led to a wave of rioting and arrests. Opposition figures fled abroad or were imprisoned; some were executed. In 1965 Mehdi Ben Barka, a prominent nationalist and opposition leader, was kidnapped in Paris and never seen again. The King's right-hand man, the Minister of Interior, Mohammed Oufkir, was linked to the disappearance, but the case was never pressed. In June of that year, Hassan dissolved Parliament and instituted a state of emergency, wielding absolute power until a new Constitution was adopted in 1970. The Constitution restored limited parliamentary government, but discontent simmered amid continuing poverty and official corruption. On July 10, 1971, King Hassan invited some 400 prominent Moroccans, diplomats and other guests to his seaside palace of Skhirat near Rabat to celebrate his 42d birthday. The festivities ended in a burst of gunfire as more than 1,000 mutinous troops attacked the palace, hurling grenades and spraying the grounds with small-arms fire. Nearly 100 guests were killed and more than 125 wounded. The King hid in a bathroom. When the firing died down, he re-emerged to find himself face to face with one of the rebel commanders. Keeping eye contact, he recited the opening verse of the Koran, and the rebel knelt and kissed his hand. Loyal troops crushed the revolt, killing more than 150 rebels and capturing 900 others, many of them young military cadets. A dozen high-ranking, conservative officers were executed. Mohammed Oufkir was named Minister of Defense. A little more than 12 months later, on Aug. 16, 1972, the King was returning from Paris aboard his private Boeing 727 when it encountered an unscheduled escort of four Royal Moroccan Air Force F-5 fighters. As the Boeing approached Rabat's airport, the fighters fired on the plane, knocking out an engine and scoring other hits. The Boeing landed safely, but the renegade pilots continued to strafe the runway until Hassan radioed them, saying the King had been killed. The rebels broke off the attack. Within hours, key participants in the coup were arrested and shot. One of their leaders proved to be General Oufkir, who apparently had been secretly involved in the earlier attack on the palace. According to official reports, the general committed suicide, but his body was supposedly found with several wounds. His widow and six children were placed under house arrest and were not released until February 1991, in an amnesty marking the King's 30 years in power. As the 1970's unfolded, the King took several steps to damp domestic turmoil. In 1973 he put through measures to increase Moroccan ownership and employment in companies doing business in Morocco and also redistributed farmland owned by foreigners to rural peasants. ''He alternated very cleverly between the kinds of reforms that would be popular with the people and the kinds of reforms popular with the ruling elite and in doing so was popular with both,'' said Mr. Pelletreau, the former American diplomat. In November 1975, in a move that would unite Moroccans against a common foe, Hassan reasserted his country's authority over the Western Sahara, a region claimed by both Morocco and Mauritania but still officially under Spanish administration, by trucking some 350,000 civilians under army escort to the region, where they staged a march. The move help secure Morocco's claim but ignited a war with guerrillas of the Polisario Front, who had been fighting for independence from Spain. Libya and Algeria supported the guerrillas in their war against the Moroccan Army. In 1984, the King signed an accord with Col. Muammar el-Qaddafi that ended Libyan backing for the insurgents. Algeria, plagued by its own domestic problems, could give them only minimal support. Militarily, Morocco eventually triumphed, agreeing to a cease-fire with Polisario in 1991 that left the country in control of most the region. The Polisario Front continues to hang on, but the United Nations is scheduling a referendum in March to determine the future of the region's 300,000 people. King Hassan was adept at managing Arab-Israeli relations, and he liked to say he viewed Morocco's Jewish population, which numbers around 8,000, as a bridge between Israelis and Arabs. During World War II his father, Mohammed V, had defied the Axis and protected his country's Jews. In 1956, the year of Moroccan independence, there were about 275,000 Jews in Morocco. Most were allowed to emigrate to Israel, Europe and elsewhere. During the Arab-Israeli wars of 1967 and 1973, King Hassan contributed a nominal number of troops to support Egypt and Syria. Nevertheless, he kept his channels open with Israel. In 1982 he was the host of a meeting of Arab leaders in Fez where he pushed through agreement on a peace plan that called for the creation of a Palestinian state with Jerusalem as its capital but implicitly recognized Israel's right to exist. The plan, though rejected by Israel, laid the groundwork for the King to meet with Prime Minister Peres in 1986, a meeting that caused the King to be criticized by Arab leaders. He responded by saying they had neither the ability to make war on Israel nor the willingness to make peace. In September 1993, Morocco gave de facto recognition to Israel by welcoming Prime Minister Rabin, marking the first official visit by an Israeli leader to an Arab nation other than Egypt. Despite such bold gestures, he was careful to play both sides of a conflict when necessary. After the Iraqi invasion of Kuwait in 1990, he sent 1,300 troops to Saudi Arabia, a gesture that pleased the West. At the time he expressed sympathy for the plight of Iraqi suffering under United Nations sanctions and ordered members of the Moroccan royal family to supervise the collection of supplies to ease their plight. Despite aid from the West, sporadic reform efforts, and an estimated $2 billion a year sent home by Moroccans working abroad, the economic situation during the 1990's remained difficult for most of Morocco's 25 million people, two-thirds of whom are under the age of 25. In the census of 1994, the urban population exceeded the rural population for the first time. Unrest generally declined in the cities, where most of the economic reforms were concentrated. Although unemployment remained a problem and the strains of a relatively slow pace of development continued, political freedom gradually increased. In the early 1990's, pressure from human rights groups reduced the number of political prisoners and reports of incidents of torture declined. In recent years, the King pushed his version of ''Hassanian democracy,'' which has widened political freedom while retaining the decisive power of the King. After elections last year, in which leftist parties gained the largest bloc of seats, the King appointed an old leftist adversary, Abdurrahman Youssufi, as Prime Minister, but the new Government has been widely criticized for failing to fulfill election promises to deal with poverty and other social problems. Like his father, the Crown Prince comes to the throne with the reputation of a playboy. At 35, he is still single in a society that emphasizes family ties. He was educated in France, where he received a degree in law and studied international relations. In addition to Arabic and French, he speaks Spanish and English. In recent years, as illness caused the King to withdraw progressively from active public life, the Crown Prince began to take a more active role in public as second in command of the Moroccan military and in helping to direct Government aid to the poor. In February the King sent the Crown Prince as his representative to the funeral of King Hussein of Jordan. With Sidi Mohammed's accession to the throne, his younger brother Moulay Rashid will become Crown Prince. In addition to his two sons, the King is survived by his wife, Lalla Latifa, a commoner who is officially described as the Mother of the Royal Children, and three daughters. Palace officials say the transition will bring no change in key Moroccan policies, including the longstanding alliance with the United States and strong support for peace between Israel and its Arab neighbors. The aides also said Sidi Mohammed would continue his father's policy of insisting on Moroccan sovereignty over the disputed territory of the Western Sahara. Nonetheless, the Crown Prince is expected to take his distance from some of his father's closest advisers, principally the Interior Minister, Driss Basri, who in his 20 years in that post has been associated with hard-line policies toward domestic opposition from both the left and the Islamic fundamentalists, who have been excluded from forming parties to compete in elections. Aides to King Hassan said recently that Sidi Mohammed was likely to move hard-liners in his father's entourage aside in effort to accelerate democratic reforms that his father had initiated in recent years. Despite his moves toward democracy, throughout his reign King Hassan remained jealous of preserving the symbols of his monarchy. He refused, for instance, to abandon the practice of having his subjects show their fealty by kissing his hand, even though advisers urged him to end it. Whether he donned a business suit to meet with Western leaders or appeared in traditional white robes to preside over religious ceremonies, the King was a leader of commanding presence, an eloquent orator in Arabic who spoke excellent French and capable English. ''His golf outings were the picture of an Oriental potentate,'' recalled Mr. Pelletreau, the diplomat. ''He would be accompanied by a vast entourage. If he wanted to sit, chairs would appear, and his guests would be offered sherbet.'' Through intelligence, charm and cunning, he steered an absolute monarchy into the modern world. ''He sheltered Morocco from the various political winds that blew across the Arab world and caused such turmoil in other countries,'' Mr. Pelletreau said.