It’s actually なんで. Basically the rule about the next (so after, not before) consonant is the correct one. Paired with some particles:
なんで
なんに
なんの
なにが
なにを
なにへ (never heard this combo, but I guess it’s possible)
なには(question words don’t pair with は)