* Shot to head kills gunmen as he scrambles from window
* Police blast apartment with explosions all night
* Two police commandos wounded in shootout (Adds details from prosecutor)
By John Irish and Nicholas Vinocur
TOULOUSE, France, March 22 (Reuters) - A 23-year-old gunman who said al Qaeda inspired him to kill seven people in France died in a hail of bullets on Thursday as he scrambled out of a ground-floor window during a gunbattle with elite police commandos.
Mohamed Merah, a Frenchman of Algerian origin, died from a gunshot wound to his head at the end of a 30-hour standoff with police at his apartment in southern France and after confessing to killing three soldiers, three Jewish children and a rabbi.
He was firing frantically at police from a Colt 45 pistol as he climbed through his apartment window onto a verandah and toppled to the ground some 5 feet (1.5 metres) below, in a suburb of the city of Toulouse, according to prosecutors and police.
Two police commandos were injured in the operation - a dramatic climax to a siege which riveted the world after the killings shook France a month before a presidential election.
"At the moment when a video probe was sent into the bathroom, the killer came out of the bathroom, firing with extreme violence," Interior Minister Claude Gueant told reporters at the scene.
"In the end, Mohamed Merah jumped from the window with his gun in his hand, continuing to fire. He was found dead on the ground."
Paris prosecutor Francois Molins said Merah had taken refuge in his bathroom, wearing a bullet-proof vest under his traditional black djellaba robe, as elite police blasted his flat through the night with flash grenades.
Police investigators were working to establish whether Merah had worked alone or with accomplices, Molins said, adding that Merah had filmed his three shooting attacks with a camera hung from his body and had indicated that he had posted clips online.
The most disturbing image of the attacks showed him grabbing a young girl at a Jewish school on Monday by the hair and shooting her in the head before escaping on a scooter.
The killings have raised questions about whether there were intelligence failures, what the attacks mean for social cohesion and race relations in France and how the aftermath will affect President Nicolas Sarkozy's slim chances of re-election.
Sarkozy called Merah's killings terrorist attacks and announced a crackdown on people following extremist websites.
"From now on, any person who habitually consults websites that advocate terrorism or that call for hate and violence will be punished," he said in a statement. "France will not tolerate ideological indoctrination on its soil."
Elite RAID commandos had been in a standoff since the early hours of Wednesday with Merah, periodically firing shots or deploying small explosives until mid-morning on Thursday to try and tire out the gunman so he could be captured.
Surrounded by some 300 police, Merah had been silent and motionless for 12 hours when the commandos opted to go inside.
Initially, he had fired through his front door at police when they swooped on his flat on Wednesday morning, but later he negotiated with police, promising to give himself up and saying he did not want to die.
By late Wednesday evening, he changed tack again, telling negotiators he wanted to die "like a Mujahideen", weapon in hand, and would not go to prison, Molins said.
"If it's me (who dies), too bad, I will go to paradise. If it's you, too bad for you," Molins quoted Merah as saying.
Merah told negotiators he was trained by al Qaeda in Pakistan and killed three soldiers last week and four people at a Jewish school on Monday to avenge the deaths of Palestinian children and because of French army involvement in Afghanistan.
In his video recording of his shooting of the soldiers, Merah cried: "If you kill my brothers, I kill you", Molins said.
Merah had staked out the first soldier he killed after replying to an advert about a scooter, investigators said on Wednesday, and had identified another soldier and two police officers he wanted to kill.
His use of his mother's computer to lure his first victim, a French soldier of North African heritage like himself, gave police a vital clue, but not in time to prevent the other killings, even though he had taken the scooter to a mechanic for a respray before the final attack on Monday.
Sarkozy's handling of the crisis could well impact an election race where for months he has lagged behind Socialist challenger Francois Hollande in opinion polls.
Early on Thursday, the first opinion poll since the school shooting showed Sarkozy two points ahead of Hollande in the first-round vote on April 22, although Hollande still led by eight points for a May 6 runoff.
Three years of economic gloom, and a personal style many see as brash and impulsive, have made Sarkozy highly unpopular in France, but his proven strong hand in a crisis gives him an edge over a rival who has no ministerial experience.
Sarkozy said an inquiry would be launched into whether French prisons were being used to propagate extremism and urged people not to seek revenge for acts he described as terrorism.
Merah has a police record for several minor offences, some involving violence, and was on the radar of French intelligence, but Gueant has said there was no evidence he had been planning radical murders.
The MEMRI Middle East think tank said he may belong to a French al Qaeda branch called Fursan Al-Izza, ideologically aligned with a movement to Islamise Western states by implementing sharia law, but Gueant said there was no evidence he formally belonged to any fundamentalist group.
Friends spoke of him as an amateur soccer player, not outwardly religious and fond of night clubs.
Merah, who had a weapons cache in his flat that included an Uzi and Kalashnikov assault rifle, boasted to police negotiators that he had brought France to its knees, and that his only regret was not having been able to carry out more killings.
French commandos had detonated three explosions just before midnight on Wednesday, flattening the main door of the building and blowing a hole in the wall, after it became clear Merah did not mean to keep a promise to turn himself in.
They continued to fire shots roughly every hour, and stepped up the pace from dawn with flash grenades.
"These were moves to intimidate the gunman who seems to have changed his mind and does not want to surrender," said interior ministry spokesman Pierre-Henry Brandet.
He was tracked down after a no-holds-barred manhunt in France, during which presidential candidates suspended their campaigning.
Immigration and Islam have been major campaign themes after Sarkozy tried to win over supporters of Le Pen, who accused the government of underestimating the threat from fundamentalism.
Leaders of the Jewish and Muslim communities have called for calm, pointing out the gunman was a lone extremist.
On Thursday, far-right candidate Marine Le Pen accused Sarkozy's government of surrendering swathes of often impoverished suburban districts to Islamic fanatics, demanding that the last month of pre-election debate put the focus back on failing security. (Additional reporting by Jean Decotte in Toulouse and Daniel Flynn, Geert de Clercq and Alexandria Sage in Paris; Writing by Catherine Bremer; editing by Philippa Fletcher)