Improved sanitizing of configuration names.

Fixes #2542