For a while now (since 8.1) unbeknownst to most people MS has moved localhost resolution into the DNS stack itself. As a result, overriding the mapping for localhost from ::1 to 127.0.0.1 can no longer be done via the hosts file.
Here's the magic one-liner (run from an elevated prompt):
netsh interface ipv6 add prefixpolicy ::ffff:0:0/96 precedence=51 label=4 store=persistent
Ping localhost and voila it should return 127.0.0.1. You can play around with the precedence to see it in action, first check the existing precedences netsh interface ipv6 show prefixpolicies you want to check the precedence of ::1/128 that's the prefixpolicy whose precedence needs to be beat by our new one. Set it below ping localhost then above.
Some recommend you issue a netsh interface ipv6 reset but in my experience on Windows 11 Pro it's not necessary.
Thanks to MatrixPost Blog for the mapping: Prefer IPv4 over IPv6 in Windows Networks - .matrixpost.net