Question: When developing a web app locally I like to use addresses like `app.localhost:8080` to access them. They work fine in Chrome but not in Safari. I guess this is because Chrome has special handling for the TLD and Safari uses the OS DNS resolver.

Is there a way to make macOS resolve all `.localhost` addresses to `127.0.0.1`?

Answer: 
Yes, as the root you should edit your /etc/hosts file and add line(s) like

```
127.0.0.1 app.localhost
127.0.0.1 app2.localhost

```

etc. 

If it doesn't work immediately then restart browser/reboot.

Update:
I have no mac, but try `127.0.0.1 *.localhost`. For my Linux it does NOT work, then the only way is to set up a DNS server on your box and configure 'localhost' zone on it.
I don't believe wildcards (\*) work in `/etc/hosts`. On MacOS Safari, the only option is a tool like [dnsmasq](https://help.ubuntu.com/community/Dnsmasq):

```bash
brew install dnsmasq
mkdir -pv $(brew --prefix)/etc/
echo 'address=/.localhost/127.0.0.1' >> $(brew --prefix)/etc/dnsmasq.conf
echo 'port=53' >> $(brew --prefix)/etc/dnsmasq.conf
sudo mkdir -v /etc/resolver
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/localhost'
sudo brew services start dnsmasq

```

If you're using a different domain extension like \*.test, replace `localhost` with `test` everywhere.

Also, port `53` is the DNS port. The remapping will work for all ports. For example, you'll be able to use `http://foobar.localhost:8080` or whatever.

The script above is modified from [this gist](https://gist.github.com/ogrrd/5831371).