220

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

BEFORE
127.0.0.1 www.example.com

AFTER
127.0.0.1:8080 www.example.com

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

RayLoveless
  • 16,084
  • 19
  • 68
  • 90
Charles Yeung
  • 36,649
  • 27
  • 83
  • 130
  • You need NGNIX or Apache HTTP server as a proxy server for forwarding http requests to appropriate application -> which listens particular port (or do it with CNAME which provides Hosting company) – Musa Feb 08 '18 at 18:08
  • https://httpd.apache.org/docs/trunk/vhosts/examples.html https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/ – Musa Feb 08 '18 at 18:16
  • Maybe review this and switch the accepted answer to the netsh answer? I ended up here when trying to figure out how to map .test domains to my docker images locally on Windows 10, and the netsh answer worked like a charm! – Jereme Feb 14 '19 at 14:55
  • The question is: are you trying to redirect a whole host to a single port (?!) or just the port 80 (of that host) to port 8080? Most answers here are on how to redirect (or at least try to redirect...;-) port 80 of a host to port 8080 of your "localhost". Example: `https://www.example.com` must 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? – ilias iliadis May 28 '20 at 18:31

11 Answers11

211

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.)

Mat
  • 188,820
  • 38
  • 367
  • 383
  • 8
    how to use bookmarks , can you lead us to a link or some documentation or tutorials about this – adham dwikat Mar 07 '17 at 09:03
  • @Jereme: you should tell that to the OP, I can't do anything about it – Mat Feb 13 '19 at 06:40
  • 1
    @Mat That would be nice, right? I just want others who see this to know to scroll down a little bit and give it a shot. I ended up here when trying to figure out how to map .test domains to my docker images locally on Windows 10, and the netsh answer worked like a charm! – Jereme Feb 14 '19 at 14:54
202

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.

Example


Overview

example.app: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.app
      • Any free address in the network 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 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=127.65.43.21 connectport=8081 connectaddress=127.0.0.1
  • I can now access the server at http://example.app

Notes:
- 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=127.65.43.21


Links to Resources:

Pau Coma Ramirez
  • 2,621
  • 1
  • 15
  • 18
  • This isn't working for me. In the link you showed it says "Also you can’t use 127.0.0.1 as connectaddress.". – Don Box Jul 08 '16 at 04:51
  • @DonBox: If you provide some more details on your setup maybe I could help you. Using **127.0.0.1** as connect address is basically the requirement in the presented case scenario because the server started at **localhost** , as do by default many other programs. If you can start at another address you don't need the whole port forwarding mechanism, you just need to modify the hosts file... Perhaps, as [detailed here](https://support.microsoft.com/en-us/kb/555744), you have not got **the ipv6 protocol** installed: use the following command to install them : `netsh interface ipv6 install` – Pau Coma Ramirez Jul 08 '16 at 13:55
  • 20
    This should be the accepted answer because the tools are already installed in my base windows 10 install and this does exactly what I needed. I was considering installing apache and mod_proxy but this is a much better solution. – Max Young Sep 14 '16 at 11:52
  • Love this solution but I cannot get it working on IIS Express. Has anyone had any success with this? – Marcus Cunningham Apr 19 '17 at 15:47
  • Hi @MarcusCunningham , I am not familiar with IIS so cannot help you there, but this solution is independent of the webserver. It is however thought for a local environment, the port mapping is local to your machine only (note the use of loopback address range 127.x.x.x). If you are attempting to perform some NAT (network address translation) you would need to do this on the router. Without more info on your case scenario, I can't give you more insight. – Pau Coma Ramirez Apr 20 '17 at 09:30
  • 2
    Doesn't work for me. Netsh seems to forward the address, but not the port. – steph643 May 15 '17 at 07:46
  • Hi @steph643 what does "netsh interface portproxy show v4tov4" show? what relevant lines do you have on your hosts file? Anything conflicting? What does 'netstat -a -n -p TCP | grep "LISTENING" ' show relevant to your addresses and 0.0.0.0 (listening to all) ? – Pau Coma Ramirez May 15 '17 at 10:45
  • I also was not able to make it work. I was trying to use `127.0.0.1` as the connect address. The last link in this answer warns that it the connect address cannot point to this. I was able to make it work by using my private ip of `10.0.75.1` since my app listens on `0.0.0.0` – gaunacode.com Nov 26 '17 at 22:46
  • 2
    I use docker's portainer. It uses localhost on port 9000. Modding the command above, I use portainer.local without having to specify a port on the address. I first, add `127.0.0.1` as the address to match `portainer.local` in my hosts file. Then I run: `netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.0.0.1 connectport=9000 connectaddress=127.0.0.1` Hit enter key in cmd/powershell to run it. After this, exit netsh by typing `exit`. In the browser, typing `http://portainer.local` now takes me to where `http://localhost:9000` used to. This worked like a charm for me. – EffectiX Jul 23 '18 at 14:33
  • @EffectiX Glad it works for you. Since you have the flexibility I would suggest that you define 127.0.0.2 (or any free one that is not 127.0.0.1, i.e. localhost) in the hosts file to match portainer.local. The command would just change the listenaddress to the new local ip 127.0.0.2 (the rest the same) and it will work just as it does now with the difference that it avoids the *collision* incase you install something else that uses localhost:80 you can continue to use both. – Pau Coma Ramirez Jul 24 '18 at 00:00
  • This works if the connectaddress is localhost, but not if it intranet or external ip. It will simply hit your local host site. Otherwise I would have some fun playing pranks on my coworkers linking Google on their machine to some porn site. I may use this though to prank my wife at some point. – user3520445 Nov 07 '19 at 19:22
  • also make sure that Services > IP Helper service is running. (it's good to make it automatic startup) – Samir Mammadhasanov Apr 20 '20 at 10:48
  • This doesn't work on the intranet, only on host computer. Any suggestions please – Kenneth mwangi Mar 02 '21 at 07:18
  • @Kennethmwangi This solution is **only for localhost**, On the Intranet, i.e. your local private network, your other nodes on your network will see your network cards assigned IP address. You will need to play with your firewall settings to allow others to access something hosted on your node. How IPs are routed on your local network is defined by your router and should be configured there. DNS server could also be implemented in your router, if that is what you are looking for. – Pau Coma Ramirez Mar 02 '21 at 07:28
185

What you want can be achieved by modifying the hosts file through Fiddler 2 application.

Follow these steps:

  1. Install Fiddler2

  2. Navigate to Fiddler2 menu:- Tools > HOSTS.. (Click to select)

  3. Add a line like this:-

    localhost:8080 www.mydomainname.com

  4. Save the file & then checkout www.mydomainname.com in browser.

Community
  • 1
  • 1
Rajat Gupta
  • 23,343
  • 56
  • 165
  • 281
  • 36
    This is a bad solution. You need to have Fiddler running at all times. – James Huckabone Jan 19 '15 at 23:17
  • 61
    @JamesHuckabone: Do you have a better solution? we'd love that! – Rajat Gupta Jan 20 '15 at 04:37
  • 16
    I get Error 400 Bad Request when i use this method – Sam Mar 12 '15 at 15:17
  • Thanks for the solution, it works. but how do i configure it to support sub domains – Xdrone May 12 '15 at 19:03
  • 3
    this no longer seems to work. Tried it with both fiddler2 and fiddler4 – basher Dec 17 '15 at 18:04
  • 1
    @basher U still can script it at Fiddler's Rules.js for sure. Hosts functionallity on Fiddler is NOT the Windows hosts files, it's a recreation of that names to make them easy to work with. Is like the Autoresponders, are just a subset of what you can do scripting it directly. If you can't do something with menus, script it. – m3nda Apr 16 '16 at 05:37
  • @erm3nda you're totally right. my bad. just played around with it again and it worked so long as fiddler was left open. Thanks. Guess i'll be leaving fiddler on 24/7 now. – basher Apr 18 '16 at 19:05
  • sorry to bump, but, it works still with Fiddler4. mapped the ip and name in window's host file, then mapped ip:port and name in fiddler4's host. – Nii Mar 21 '19 at 17:37
  • Bad Request - Invalid Hostname HTTP Error 400. The request hostname is invalid. This answer worked for me: https://stackoverflow.com/a/30817890/732673 – Josh Noe May 08 '19 at 19:01
  • Does not work. Getting this error and Fiddler is crashing. [1] 8102 abort (core dumped) mono /home/curtis/Desktop/vjbl/fiddler-linux/Fiddler.exe Unable to attach: program terminated with signal SIGABRT, Aborted. No threads. – kwoxer Jul 10 '19 at 11:40
15

Fiddler2 -> Rules -> Custom Rules

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

if (oSession.HostnameIs("mysite.com")){
    oSession.host="localhost:39901";
}
Sergey
  • 4,403
  • 3
  • 28
  • 40
  • Indeed Fiddler is so much powerfull than expect to not run a webserver on port 80, or even Skype... On Windows works better than in Linux with Mono, at least the capturing features, cuz Linux do not uses Inet from CLRL. This is a nicer way to do things on Fiddler, cuz it's exportable with 1 file. – m3nda Apr 16 '16 at 05:35
6

The simplest way is using Ergo as your reverse proxy: https://github.com/cristianoliveira/ergo

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.

  • 6
    Ergo appears to be a project you worked on (based on the username). If that is the case, you should indicate more explicitly that you are affiliated with that product. See [this answer](https://meta.stackexchange.com/a/59302/337783) as well as the other answers. – Frank Tan Jul 06 '18 at 14:42
  • @FrankTan [ergo](https://github.com/cristianoliveira/ergo) is **NOT** a product for sale and it's licensed under [MIT License](https://github.com/cristianoliveira/ergo/blob/master/LICENSE); he doesn't even have a GitHub sponsorship running, so, it's totally fine to include it as an answer without mentioning that he is the creator of this utility. – Christos Lytras Feb 23 '21 at 02:36
2

Using netsh with connectaddress=127.0.0.1 did not work for me.

Despite looking everywhere on the internet I could not find the solution which solved this for me, which was to use connectaddress=127.x.x.x (i.e. any 127. ipv4 address, just not 127.0.0.1) as this appears to link back to localhost just the same but without the restriction, so that the loopback works in netsh.

aards
  • 43
  • 6
  • I suggest you to remove the first and the last paragraphs. Just tell that existing solutions did not work and you found another one. Otherwise your answer looks a lot like a comment, so people might flag it as "not an answer" and it will be removed. – StaceyGirl Jan 14 '18 at 14:38
1

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

DogBot
  • 428
  • 1
  • 6
  • 15
1

-You can use any free address in the network 127.0.0.0/8 , 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 ) :

127.0.0.5 flask.dev
  • Make sure the port is the default port "80" in my case this is what in the python flask: app.run("127.0.0.5","80")

  • now run your code and browse flask.dev

0

You need NGNIX or Apache HTTP server as a proxy server for forwarding http requests to appropriate application -> which listens particular port (or do it with CNAME which provides Hosting company). It is most powerful solution and this is just a really easy way to keep adding new subdomains, or to add new domains automatically when DNS records are pointed at the server.

Musa
  • 2,120
  • 23
  • 22
0
  1. Install Redirector
  2. Click Edit redirects -> Create New Redirect

enter image description here

Smart Manoj
  • 3,837
  • 2
  • 24
  • 45
0

This doesn't give the requested result exactly, however, for what I was doing, I was not fussed with adding the port into the URL within a browser.

I added the domain name to the hosts file

127.0.0.1      example.com

Ran my HTTP server from the domain name on port 8080

php -S example.com:8080

Then accessed the website through port 8080

http://example.com:8080

Just wanted to share in case anyone else is in a similar situation.

Danbardo
  • 306
  • 2
  • 10