BIN JAWWAD, Libya — Libyan helicopter gunships fired on a rebel force advancing west toward the capital along the Mediterranean coastline Sunday and forces loyal to leader Moammar Gadhafi fought intense ground battles with the rival fighters.

The opposition force pushed out of the rebel-held eastern half of Libya late last week for the first time and has been cutting a path west toward Tripoli. On the way, they secured control of two important oil ports at Brega and Ras Lanouf and by Sunday, the rebels were advancing farther west when they were hit by the helicopter fire and confrontations with ground forces.

The uprising against Gadhafi, which began just days after President Hosni Mubarak was ousted by protesters in neighboring Egypt, has been sliding rapidly toward civil war, making it the bloodiest episode in the Middle East's wave of unrest.

The seesawing battles for towns and oil installations along the coastline signaled that Libya's fighting could be prolonged, compared with the ousting of Mubarak after just 18 days. The protesters-turned-rebels — backed by mutinous army units and armed with weaponry seized from storehouses — are going on the offensive to try to topple Gadhafi's 41-year-old regime. At the same time, pro-Gadhafi forces have conducted counteroffensives to try to retake the oil port of Brega and the rebel-held city of Zawiya west of Tripoli — where bloody street battles were reported over the weekend.

In Sunday's fighting, AP reporters on the scene said government airstrikes hit the town of Ras Lanouf, and ground troops loyal to Gadhafi retook the town of Bin Jawwad, about 110 miles (160 kilometers) east of Gadhafi's hometown and stronghold of Sirte, which could prove to be a decisive battleground.

From the edge of Bin Jawwad, a steady barrage of rockets and artillery fired by pro-Gadhafi forces thumped to the ground. About 50 rebel fighters were trapped inside a mosque, and their comrades who had retreated to the edge of the city sent 20 pickup trucks back through the bombardment to try to rescue them. One of the trucks was hit.

A warplane attacked a small military base at Ras Lanouf and destroyed three hangars and a small building. Regime forces shelled rebel positions there with rockets and artillery. Ambulances sped toward the town and rebels moved trucks and four multiple-rocket launchers toward the front lines.

Four people were killed in the fighting in those two towns, and a French journalist for France 24 TV was wounded, hospital officials said.

In Tripoli, the city of 2 million that is most firmly in Gadhafi's grip, residents awoke before dawn to the crackle of unusually heavy and sustained gunfire that lasted for at least two hours. Some of the gunfire was heard around the sprawling Bab al-Aziziya military camp where Gadhafi lives, giving rise to speculation that there may have been some sort of internal fighting within the forces defending the Libyan leader inside his fortress-like barracks. Gadhafi's whereabouts were unknown.

The U.S. has moved military forces closer to Libya's shores to put military muscle behind its demand for Gadhafi to step down immediately. But Washington has expressed wariness about talk of imposing a "no fly" zone over the North African nation to prevent the Libyan leader from using his warplanes to attack the population.

At the same time, the U.N. has imposed sanctions, and Libya's oil production has been seriously crippled by the unrest. The turmoil has caused oil prices to spike on international markets.

Some of the day's heaviest fighting was over the city of Misrata, where residents said pro-Gadhafi troops punched into the city with mortar and tank artillery and were pushed out five hours later by rebel forces. Misrata is 120 miles (200 kilometers) east of Tripoli.

Rebel military commanders intentionally opened the way for the government tanks to enter the city, then surrounded them and attacked the armor with anti-aircraft guns and mortar shells, said Abdel Fatah al-Misrati, one of the rebels.

"Our spirits are high," he said. "The regime is struggling and what is happening is a desperate attempt to survive and crush the opposition, but the rebels are in control of the city."

"Now the Gadhafi forces are trapped inside the city," he said.

A doctor reached by The Associated Press in the city's main hospital said tanks shelled its stores of medical supplies, setting them on fire.

The residents said the shelling was almost over by early afternoon.

The residents and the doctor spoke on condition of anonymity because they feared reprisals.

Four rebels and five pro-Gadhafi troops were killed, al-Misrati said.

Hundreds, perhaps thousands, have died since the uprising began on Feb. 15. Tight restrictions on media make it near impossible to get an accurate tally.

The rebels headquartered in the main eastern city of Benghazi have set up an interim governing council that is urging international airstrikes on Gadhafi's strongholds and forces.

British Defense Secretary Liam Fox said Sunday that a "small British diplomatic team" is in eastern Libya to try to talk to rebels. But he would not comment on a report that special forces soldiers were captured by Gadhafi opponents when a secret mission to put British diplomats in touch with leading rebels went awry.

British Foreign Minister William Hague urged Gadhafi to hand over power and put an "immediate stop" to the use of armed force against Libyans.

In Tripoli, Libyan authorities tried to explain the unusually heavy gunfire that erupted before dawn by saying it was a celebration of the regime taking back Ras Lanouf and Misrata, though both places remained in rebel hands late Sunday afternoon.

After the gunfire eased in the early morning, thousands of Gadhafi's supporters poured into Tripoli's central square for a rally, waving green flags, firing guns in the air and holding up banners in support of the regime. Hundreds drove past Gadhafi's residence, waving flags and cheering. Armed men in plainclothes were standing at the gates, also shooting in the air.

Britain's Sunday Times newspaper reported that up to eight British special forces soldiers, armed but in plain clothes, were captured while escorting a junior British diplomat through rebel-held territory in eastern Libya. It said the special forces intervention angered Libyan opposition figures who ordered the soldiers to be locked up on a military base.

The British Defense Secretary Fox said his government was in touch with the team in Benghazi but told BBC radio it would be "inappropriate" to comment further. When pressed on whether the U.K. diplomatic team was in danger, Fox reiterated that the government is in contact with the diplomatic team.

"It is a very difficult situation to be able to understand in detail," he said. "There are a number of different opposition groups to Colonel Gadhafi in Libya who do seem relatively disparate."

British Foreign Minister Hague repeated the international community's demand for Gadhafi to step down.

"Given the continued levels of illegitimate violence within Libya we call upon Colonel Gadhafi to put an immediate stop to the use of armed force against the Libyan people," Hague said in a statement. "He must hand over power without delay to a government which fully recognizes the legitimate aspirations of the Libyan people," he added.

"The U.K. reiterates its support for the transition to a government that will deliver greater democracy, justice, transparency, human rights and accountability in Libya. We understand the desire of Libyans to enjoy the freedoms which have been denied to them for many years and support them in this endeavor," he said.

Michael reported from Tripoli.