Using port number in Windows host file


After installing TeamViewer, I have changed the wampserver port to 8080, so the address is http://localhost:8080.

For the host file located at C:\WINDOWS\system32\drivers\etc\, I have also made the change as below



When I access, it doesn't redirect to my wampserver, how can I fix it?

8/20/2019 3:11:21 PM

Accepted Answer

The hosts file is for host name resolution only (on Windows as well as on Unix-like systems). You cannot put port numbers in there, and there is no way to do what you want with generic OS-level configuration - the browser is what selects the port to choose.

So use bookmarks or something like that.
(Some firewall/routing software might allow outbound port redirection, but that doesn't really sound like an appealing option for this.)

12/28/2011 7:15:14 AM

I managed to achieve this by using Windows included Networking tool netsh.

As Mat points out : The hosts file is for host name resolution only, so a combination of the two did the trick for me.


 |                           <--Link by Hosts File
       |                     <--Link by netsh Utility
       +--> localhost:8081


  • Started my server on localhost:8081
  • Added my "local DNS" in the hosts file as a new line
      • Any free address in the network (127.x.x.x) can be used.
      • Note: I am assuming is not occupied by another service.
      • You can check with netstat -a -n -p TCP | grep "LISTENING"
  • added the following network configuration with netsh command utility
    • netsh interface portproxy add v4tov4 listenport=80 listenaddress= connectport=8081 connectaddress=
  • I can now access the server at

- These commands/file modifications need to be executed with Admin rights

- netsh portproxy needs ipv6 libraries even only to use v4tov4, typically they will also be included by default, otherwise install them using the following command: 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=


Fiddler2 -> Rules -> Custom Rules

then find function OnBeforeRequest on put in the next script at the end:

if (oSession.HostnameIs("")){"localhost:39901";

The simplest way is using Ergo as your reverse proxy:

You set your services and its IP:PORT and ergo routes it for you :).

You can achieve the same using nginx or apache but you will need to configure them.


If what is happening is that you have another server running on localhost and you want to give this new server a different local hostname like http://teamviewer/

I think that what you are actually looking for is Virtual Hosts functionality. I use Apache so I do not know how other web daemons support this. Maybe it is called Alias. Here is the Apache documentation:

Apache Virtual Hosts examples


-You can use any free address in the network , in my case needed this for python flask and this is what I have done : add this line in the hosts file (you can find it is windows under : C:\Windows\System32\drivers\etc ) :
  • Make sure the port is the default port "80" in my case this is what in the python flask:"","80")

  • now run your code and browse


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]