This answer is quite simple: learn from the French
France held a solid colonial empire until not so long ago. Then WWII happened. In 1940, France surrendered to Germany. History books gladly tell the tale of Charles de Gaulle going to England to fight the good fight. These books are light on the fate of the colonies. Here are two cautionary tales.
TL;DR: Independence is inevitable. Or when someone takes arms to fight for their independence, there's only two things you can give them: liberty or death.
Before Vietnam there was French Indochina. In 1940, the Vichy regime, being now an ally of Nazi Germany, got to maintain administration over its colonies, and in particular Indochina, even as it became a de facto Japanese military occupation. After the liberation of France in 1944, the Japanese decided to seize control of Indochina for fear that the colonial government of France would start to work against them.
The Viet Minh was initially created to fight against the French Empire, and during WWII fought the Japanese occupation, with the support of the US. The Viet Minh kicked the Japanese out and declared the independence of Vietnam. This was short-lived as a victorious France came back to reclaim its colony. This lead to the Indochina War, that opposed the Viet Minh, now supported by China, against France, with support of the US.
The Indochina War lasted from 1946 to 1954, and ended after an humiliating defeat at Dien Bien Phu. The Viet Minh had managed to transport artillery across the dense Vietnamese jungle and assemble them all around a French military camp. While they took heavy losses themselves, they forced the French commanders to surrender. French Indochina was replaced by Cambodia, Laos and North and South Vietnam.
The Vietnam War was the logical sequel to the conflict. The leaders of the Viet Minh, now leaders of North Vietnam, weren't satisfied with the split between North and South. And like the Viet Minh against the Japanese and the French, the Viet Cong and the North Vietnamese Army used guerilla tactics against South Vietnam and the US, and won.
Algeria was a French colony in 1940. It remained under French rule, Vichy French rule, with the French army defending it against Free France and the Allies. But while the government collaborated, indigènes, Native-Algerians if you will, were not particularly interested in helping the Vichy regime round up Jews. Nationalists were split. There were those that wholeheartedly supported the Allies. There were others that considered that their fight was independence, not anti-fascism.
When Algeria was liberated and Alger became the capital of Free France until the liberation of Paris, nationalists had hopes this would mean recognition from the French. It did not. The anti-colonial sentiment grew for the rest of WWII. On May 8, 1945, the day Nazi Germany surrendered, at least 6000 Algerians were massacred by the French army. This planted the seed that, 9 years later in 1954, became the Algerian War of Independence.
The war was brutal, nationalists faced against a French military resolute to not lose another war. This wasn't a far away land like Indochina, for all intents and purposes it was France. The French army and Algerian loyalists outnumbered Algerian nationalists. Like in Indochina, the Algerian War was asymmetric. Nationalists fought a guerilla war against a regular army.
The conflict saw the end of the Fourth French Republic, and the beginning of the Fifth. The Algerian National Liberation Front kept fighting. Eventually, the French negotiated an end to the war, and in 1962 Algeria took its independence.
The French did the same critical mistake twice: they thought they could fight the will of the people with tanks. They should have known better, they had just ousted the superior Germans force out of their home.
WWII exacerbated the anti-colonial sentiments in those countries, and the way France handled it post-war was by sending troops. It just served to widen the divide. Guerilla tactics proved more than effective against a conventional army that was used to fight a conventional enemy. But that's not why the French lost these war. They lost because, from the very beginning, they never had the hearts and minds of the people.
After decades of an average colonial rule, which is to say far from bloodless, the French found that the people they ruled over overseas simply didn't like them very much. And as the wars dragged on, the bodies piled up and reports of exactions came up, the French people also started to find they didn't like that line of action very much. Indochina and Algeria proved to be more trouble than was worth.
The home field advantage is a supreme tactical and strategic advantage. But in the end, the side willing to sacrifice everything for their own land won.