BEIRUT — The Syrian military on Thursday captured a strategic northern town held by rebels for the past five years, marking another milestone in the government’s long war to stamp out the insurgency against President Bashar al-Assad and producing new misery for the people forced to flee.

The seizure of Khan Sheikhoun in embattled Idlib province also cut off a garrison of Turkish soldiers at an observation post in the town of Morek to the south, along with a cluster of largely depopulated villages still held by the rebels. Idlib is the country’s last significant area controlled by the opposition.

Videos posted by pro-government and Russian news sites showed Syrian soldiers moving into the abandoned streets of the town, past scorched buildings, blown-out storefronts and rubble-strewn streets. “A small tour of Khan Sheikhoun with our friends; it’s cute,” said a grinning Russian man, wearing military uniform but apparently working as a journalist, as he toured the town in a military vehicle.

Khan Sheikhoun straddles a major north-south highway and has stood on the front line of the conflict throughout the eight-year revolt against Assad. The town changed hands three times before the rebels finally secured control in 2014, each battle bringing more destruction. In 2017, it was the site of one of the worst sarin gas attacks by the Syrian government, prompting President Trump to order the first of two U.S. strikes against Syrian chemical weapons facilities. 

The government advance came after a four-month offensive and at enormous cost in civilian lives. More than 500 people, including many children, had been killed as of Aug. 8, according to figures from the United Nations, most of the casualties occurring in Syrian and Russian airstrikes targeting towns and villages far from the front lines. 

More than half a million people have been displaced from their homes, 76,000 of them in the past week, fleeing incessant bombardments for the relative safety of areas closer to the Turkish border, according to the United Nations. Families are camping out along roads without tents or shelter from the scorching summer heat, said Abdul-Karim Omar, a member of the Idlib provincial council who visited the area.

“The situation is catastrophic,” he said. “People left without their clothes. I saw children without shoes or food begging for a mattress to lie down. They have nothing over their heads. They are sleeping under open skies.” 

Throughout most of the offensive, the front lines barely moved, with loyalist forces appearing unable to advance against stiff resistance from the opposition, composed of a variety of rebel groups including the powerful extremist group Hayat Tahrir al-Sham — formerly known Jabhat al-Nusra and linked to al-Qaeda.   

Over the past week, the tide turned, and the Syrian army broke through rebel lines, buoyed by an injection of fresh troops and Russian advisers, according to military analyst Kamal Alam, who monitors the Syrian army.

Exhausted rebel and Islamist militant fighters ran out of ammunition and scattered into the surrounding countryside, said Mohammed Ghaleb, a spokesman for one of the rebel factions. 

The bloodshed of recent weeks comes as a reminder that the war in Syria is far from over, even though the outcome is no longer in doubt. Assad has secured his survival in the capital, Damascus, but most of the province of Idlib and small slices of three adjoining provinces remain outside his grasp. An estimated 3 million to 4 million people live there, at least half of them refugees from battles elsewhere in the country who fled rather than submit to the restoration of government rule.

An agreement reached between Russian President Vladimir Putin and Turkish President Recep Tayyip Erdogan last year sought to impose a cease-fire intended to allow time for the negotiation of a wider political settlement. The agreement created a demilitarized zone between the warring parties, encompassing Khan Sheikhoun, monitored from Turkish military observation posts.

But Turkey proved unable to persuade either the rebel fighters or the Islamist militants to comply with the terms of the agreement, including the withdrawal of heavy weapons from the demilitarized zone. Russia has grown increasingly frustrated with Turkey’s failure to achieve compliance with the agreement. 

It remains unclear how much farther the Syrian government will take its offensive into opposition-held territory. Airstrikes over the past day against the town of Maarat al-Numan, which lies to the north along the M5 highway, suggests it may be the next target.

Assad has always insisted that he intends to retake every inch of Syria by force before considering any political settlement. But the government remains constrained by any negotiations that take place between Russia and Turkey, which are pursuing a separate alliance to secure interests that transcend Syria, Alam said. “The Syrians would like to keep it up, but it’s determined by how Russia and Turkey negotiate,” he said.