Migrants walk behind a temporary protective fence at the border between Hungary and Serbia near Morahalom, southeast of Budapest, in February. (Zoltan Gergely Kelemen/AP)

Under cover of darkness, the taxis pulled up at the edge of this forlorn border town — smugglers at the wheel, migrants in the passenger seats and the next stage of the journey through Europe looming beyond the moonlit trees.

Go, the smugglers said. Hungary isn’t far.

But when the migrants had finished hacking their way through a dense woodland, they found their paths blocked by a 13-foot, razor-wire-topped fence, with snarling German shepherds and pepper-spray-toting Hungarian police standing watch on the other side.

“Go away!” Musa Jabar-Khel, a slender 22-year-old Afghan, recalled the police yelling. “We don’t like migrants here. We hate them.”

That much was clear last fall, when Hungary went to great lengths to earn its reputation as the most hostile nation in Europe toward the unparalleled stream of humanity fleeing across the continent.

After the government built its fence and announced three-year prison sentences for anyone who dared to cross it, the refugee trail instantly diverted away from Hungary and toward the country’s more hospitable neighbors.

Now that the neighbors are all closing their own borders, however, asylum seekers are coming back to Hungary. After months in which the number of people caught trying to sneak through the fence dropped nearly to zero, arrests have risen sharply in recent weeks as controls tightened elsewhere. In February alone, nearly 2,500 people were apprehended.

The surging numbers daring to enter Hungary — despite the harsh consequences — reflect just how desperate conditions have grown for those seeking a path to Western Europe as doors slam shut all along the trail.

European Council President Donald Tusk declared Wednesday that the most common route for migrants into Europe had “come to an end” after Macedonia, Serbia, Croatia and Slovenia all announced they would require European passports or visas for anyone seeking entry.

The moves effectively stranded 30,000 people farther south in Greece, including 13,000 at the country’s Macedonian border. Heavy rain added to the misery Wednesday as families tried to huddle under tents or makeshift shelters.

At other border bottlenecks, aid workers reported that crowds were thinning — suggesting that at least some people may be giving up.

Journey alongside refugees through Lesbos, the gateway to a new life

But others are undoubtedly turning to smugglers to find alternative routes. As the return of migrant flows to Hungary shows, fences may not be as effective when everyone is building one.

“Hungary’s fence is not the solution to Europe’s problems,” said Erno Simon, a Budapest-based spokesman for the Office of the U.N. High Commissioner for Refugees. “It derailed the flow of people for some months. But even as more and more obstacles emerge, people continue to come and try to find other ways.”

Indeed, Simon said, the erection of fences and other barriers will not stop the flows; it will probably just fragment them, sending people fleeing war, oppression and poverty off the well-worn paths and onto ever-more dangerous routes. That, in turn, will only make it harder for aid groups to help those in need, while driving up the demand for smugglers.

“The more borders that are closed,” Simon said, “the higher the price that smugglers can charge.”

Nonetheless, slamming shut Europe’s famously open borders is increasingly the weapon of choice for continental leaders seeking to cope with an influx of asylum seekers that topped 1 million last year — and that, at least until this week, was on course to be much higher in 2016.

When Hungarian Prime Minister Viktor Orban ordered fences built along the country’s southern flank last year — justifying them as a necessary defense against a Muslim “invasion” — he was widely derided by his fellow European leaders.

But since then, attitudes have hardened continent-wide. At a European Union summit this week, blocking the migrants’ path became official policy. Europe even threatened to send those who arrive in Greece by boat straight back to Turkey.

The new approach is intended to obliterate a route that once sped people to their destinations of choice in Western Europe. For Hungary, it represents a vindication.

“We didn’t tell them to build these fences. They just used their own common sense,” said Hungarian government spokesman Zoltan Kovacs. “Europe simply cannot take responsibility for all human suffering around the world.”

Kovacs acknowledged that the new obstacles in neighboring countries were driving people back to Hungary. But he said the solution was for Hungary to toughen its defenses.

As the number of people slipping through the country’s border with Serbia has grown in recent weeks, Orban has ordered increased police and military patrols. Kovacs said the government was also making plans to build a fence along its border with Romania as it seeks to head off an emerging route from the east.

“The goal is to send a clear message telling people not to try to come through Hungary,” he said. “It’s not going to work.”

And yet, people are trying every day — so many, in fact, that the country’s detention centers are packed well past capacity. With nowhere else to put people, the government releases detainees onto the streets, effectively allowing them to travel on to Austria through a border that remains wide open.

“The government is putting up a very strong facade — trying to deter people through language and law,” said Marta Pardavi, co-chair of the Hungarian Helsinki Committee, a human rights group. “But in fact those who do cross will eventually be able to continue their journeys. There may just be a collateral cost of a few weeks or months of detention.”

That cost often piles even more misery upon people who have already experienced a lifetime’s worth.

Feras Turkmani fled the war in his native Syria last fall, with plans to go to Germany and continue his studies in English literature. Instead, the shy and slim 28-year-old was arrested minutes after crossing into Hungary — a country he had planned to travel through as quickly as possible. He spent the next five months in a detention center for asylum seekers that he likened to a prison.

“I didn’t think it would be like this in Europe,” said Turkmani, who cited as his favorite books “A Streetcar Named Desire” and “Heart of Darkness.” “I asked the guards for books. But they only had ones in Hungarian.”

Turkmani said he was able to speak with his parents just once a week while he was held. Pardavi, the human rights advocate, said her organization is contacted daily by anxious families who have lost touch with relatives stuck in Hungary’s labyrinthine detention system.

Under the law, everyone arrested for illegally crossing the fence is entitled to a trial. In the southern Hungarian city of Szeged, a special court is in session nearly every day, and it churns out convictions with a rare efficiency.

Under the watchful eyes of camouflage-garbed troops armed with assault rifles, the defendants are brought in with their wrists shackled by iron handcuffs. They then appear before a judge, who reads out what he describes as the defendants’ confession to the crime of having illegally crossed Hungary’s border fence.

On a recent day, two young Algerian men appeared before the court wearing threadbare clothes and shoes without laces. They each testified that they had no job and no money.

The stern-faced prosecutor told the court that their crime is punishable by a prison term. But she urged leniency. “Their presence in Hungary is not desirable,” she said. “I ask instead for expulsion.”

After about five minutes of deliberation, that was the sentence the judge gave them — along with an order to each repay about $80 in court costs.

In reality, few are actually expelled; once migrants have crossed into Hungary, Serbia refuses to take them back. So instead they are held in detention facilities until the government decides to release them.

Asylum seekers traveling north through the Balkans often know little of what awaits them in Hungary. They just know that other routes are blocked and that smugglers are offering an alternative.

Jabar-Khel, the 22-year-old Afghan who tried to sneak across the border but was ordered away from the fence by Hungarian police, said he had initially tried to travel north through Croatia along a route traversed by tens of thousands of migrants in recent months. But when he arrived at the Serbian-Croatian border, Afghans were not allowed to pass.

So instead he paid a Serbian smuggler around $400 to take him to the Hungarian border. He traveled with dozens of others in a convoy of smuggler-driven taxis. The smugglers did not warn them about the forbiddingly tall fence — or about the heavy police presence on the other side.

Some ultimately made it across, only to be captured. For two days and nights, Jabar-Khel tried to find a way through the fence. But the police seemed to anticipate his every move. He eventually gave up and, with two friends, retreated to the local bus station to consider their next move. There were no obvious answers.

“Afghanistan has been bombed. Here it is closed. There it is closed,” said one of the friends, 37-year-old Nazir Ahmadzai, whose coat had been shredded by a failed attempt to hurdle the barbed wire. “So where should we go?”

Anthony Faiola in Berlin and James McAuley in Paris contributed to this report.

Read more:

When Britain votes on the E.U., Western security could be on the line

European leaders strike deal to try to keep Britain in the E.U.

Spring could bring a fresh surge of refugees. But Europe isn’t ready for them.

Today’s coverage from Post correspondents around the world