I often see these two used interchangeably.
Problem:
“A while” is not the same as “awhile”.
Explanation:
The noun “while” means period or interval of time.
The noun phrase “a while” means a short time, so one correctly can say “He stayed for a while.”, which means He stayed for a short time., or one correctly can say “He stayed a while.”, which means He stayed a short time..
The adverb “awhile” means for a short time. Notice the “for” in the definition of “awhile”. So the meaning of “He stayed awhile.” would be He stayed for a short time. And it would be INCORRECT to write “He stayed for awhile.” because one would then have two “for”s in the translation: He stayed for for a short time.
Solution:
Use “a while” when you need a noun phrase; use “awhile” when you need an adverb.