I managed to achieve this by using Windows builtin networking tool netsh.
As Mat points out: The hosts file is for hostname resolution only, so a combination of the two did the trick for me.
Example
Overview
example.test:80
| <--Link by Hosts File
+--> 127.65.43.21:80
| <--Link by netsh Utility
+--> localhost:8081
Actions
Started my server on localhost:8081
Added my "local DNS" in the hosts file as a new line
127.65.43.21 example.test
- Any free address in the subnet
127.0.0.0/8 (127.x.x.x) can be used.
- Note: I am assuming
127.65.43.21:80 is not occupied by another service.
You can confirm this by checking the output of netstat -a -n -p TCP | grep "LISTENING"
Added the following network configuration, using netsh:
netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.65.43.21 connectport=8081 connectaddress=127.0.0.1
I can now access the server at http://example.test
Notes:
- These commands/file modifications need to be executed with Admin rights
- netsh portproxy needs IPv6 libraries, even just to use
v4tov4. Typically these will be installed by default, otherwise install with netsh interface ipv6 install
You can see the entry you have added with the command:
netsh interface portproxy show v4tov4
You can remove the entry with the following command:
netsh interface portproxy delete v4tov4 listenport=80 listenaddress=127.65.43.21
Domains forced to HTTPS via preloaded HSTS
I recently had the problem with a webserver that does not support https, only http, and I was constantly getting blocked
My Work-Around has been to use a name without a tld or one that is not known and use the forward slash at the ending. e.g.
hosts file:
127.0.1.101 aw

In Any case you can check if HTTPS is forced visiting
chrome://net-internals/#hsts
and Query HSTS/PKP domain , Result should be not Found
Links exposing attempts at certain workarounds:1, 2
These Workarounds attemp to add exceptions which should be able to be viewed in Insecure Sites and possibly also in the Redirection configuration sections
Here you will find a list of TLDs forced to HTTPS
Links to Resources:
https://www.example.commust go to port 8080? or to the default 443?(as it should) Which means: do you want a https also be redirected to port 8080 of your localhost? Or you do not care at all where this will be redirected?