How do I avoid messing up URLs when switching from example.com/subdir to example.com?

You can search and replace every occurence of “http://example.com/subdir” in the database with “http://example.com“.

If you’re comfortable with WP-CLI, there is command for that:

$ wp search-replace 'http://example.com/subdir' 'http://example.com'

If not, you can use some plugin for that. Some possible would be: