Awhile as one word is an adverb meaning "for a short time" — stay awhile. A while as two words is a noun phrase and is what you need after a preposition — for a while, in a while. Because awhile already contains "for", "for awhile" is redundant.
The core difference
An adverb with "for" built in, and a noun phrase that needs one.
- awhile (adverb) — for a short time: rest awhile.
- a while (noun phrase) — after a preposition: for a while, after a while, in a while.
The redundancy
Awhile means "for a while", so for awhile says "for for a while". That is why it is flagged as an error — not because the word is wrong, but because the preposition is already inside it.
The reliable test
If a preposition (for, in, after) comes before it, use two words. If the word stands alone after a verb, one word works — though a while is also acceptable there, which makes the two-word form the safer default.