Or is it just a whimsical saying?
I cannot evaluate the full truth of this claim, but Field Marshal Montgomery claimed to have invented a similarly phrased rule in 1968. The first record of him speaking it is from 1962 to the House of Lords, but he claimed to have said it before then. In that case, he is probably referring to the experience of WW2, where Germany was broken in Russia and Japan in China, and trying to craft it into a larger world-historical claim. Montgomery, for example, said to the House of Lords in 1962.
Rule 1, on page 1 of the book of war, is: "Do not march on Moscow". Various people have tried it, Napoleon and Hitler, and it is no good. That is the first rule. I do not know whether your Lordships will know Rule 2 of war. It is: "Do not go fighting with your land armies in China". It is a vast country, with no clearly defined objectives.
In 1968, he said of Vietnam,
The United States has broken the second rule of war. That is: don't go fighting with your land army on the mainland in Asia. Rule One is, don't march on Moscow.
However, there are some people who claim that Montgomery took the phrase from another WW2 commander, everyone from Eisenhower to Stalin to a raft of less famous personages. But Montgomery is the first person to say it on record, as far as I am aware. Regardless, the timing of the rule in the aftermath of WW2 doesn't prove that's the war that inspired it. But it certainly gestures suggestively in that direction.
From House of Lords Debate, Montgomery, and The New York Times.
That phrase is likely not a reference to a particular war or event, but to the "Second Rule of War", which was claimed to have been invented by the Viscount Bernard Montgomery, the famed British general of the Second World War, although it might be possible that another WW2 General, MacArthur, may have expressed similar sentiments at an earlier date. Montgomery claimed responsibility for defining that rule of war twenty-five years before it was popularised in The Princess Bride (1987), stating before the House of Lords on May 30, 1962:
Rule 1, on page I of the book of war, is: "Do not march on Moscow". Various people have tried it, Napoleon and Hitler, and it is no good. That is the first rule. I do not know whether your Lordships will know Rule 2 of war. It is: "Do not go fighting with your land armies in China". It is a vast country, with no clearly defined objectives
Monty was also quoted giving a version of Rule 2 in the New York Times in July 1968 in an article titled "Montgomery Calls on U.S. to Write Off the War in Vietnam":
The United States has broken the second rule of war. That is: don't go fighting with your land army on the mainland in Asia. Rule One is, don't march on Moscow. I developed those two rules myself.
It is, however, worth noting that Monty may have plagiarised Rule 2: Arthur Schlesinger's 1965 Biography of Kennedy suggests that MacArthur may have said "Anyone wanting to commit American ground forces to the mainland of Asia should have his head examined" in 1961. This isn't exactly the same phrase, but contains a similar sentiment that land wars in Asia are a foolish venture, and would predate Monty's first recorded utterance of his Rule 2.
Sources: House of Lords Archives (Hansard)
Arthur Schlesinger's A Thousand Days: John F. Kennedy in the White House Google Books Amazon