Using port number in Windows host file [closed]
Asked Answered
W

11

306

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?

Wriggle answered 28/12, 2011 at 7:9 Comment(4)
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)Xmas
httpd.apache.org/docs/trunk/vhosts/examples.html nginx.com/resources/wiki/start/topics/examples/server_blocksXmas
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!Beguile
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?Luannaluanne
B
231

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

Bartlett answered 28/12, 2011 at 7:15 Comment(3)
how to use bookmarks , can you lead us to a link or some documentation or tutorials about thisSpanos
@Jereme: you should tell that to the OP, I can't do anything about itBartlett
@Bartlett 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!Beguile
C
345

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

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

Links to Resources:

Club answered 15/4, 2016 at 12:8 Comment(20)
This isn't working for me. In the link you showed it says "Also you can’t use 127.0.0.1 as connectaddress.".Gamic
@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, you have not got the ipv6 protocol installed: use the following command to install them : netsh interface ipv6 installClub
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.Infold
Love this solution but I cannot get it working on IIS Express. Has anyone had any success with this?Frogmouth
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.Club
Doesn't work for me. Netsh seems to forward the address, but not the port.Patten
Hi @Patten 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) ?Club
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.0Recant
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.Propjet
@Propjet 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.Club
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.Crine
also make sure that Services > IP Helper service is running. (it's good to make it automatic startup)Wondawonder
This doesn't work on the intranet, only on host computer. Any suggestions pleasePrincipium
@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.Club
Thanks, exactly what I was looking for. I have forwarded 127.0.1.1:80 to 192.168.0.100:8080 (virtual machine running on linux server, port 80 used by its own http server).Monolingual
Great solution, but I had to do "netsh interface ipv4 install" to make it work, otherwise nothing happens.Nitrile
Default answer may be outdated depending on whether you're using ipv4 or ipv6 routing now: netsh interface portproxy add v4tov6 listenport=80 listenaddress=127.65.43.21 connectport=8081 connectaddress=::1 works for me.Carrero
Just a heads up for anyone who does decide to do this -- it may be hard to remember what you did several years later when you get a new computer! Or if you're trying to get someone else set up on your project. So bear that in mind as a tradeoff for the relative ease of getting this set up vs e.g. nginx where you could commit a config file to your repo.Frieze
This is not working for me. My service is up and running on 127.0.0.1:10000. I try "netsh ... listenport=8080 listenaddress=127.65.43.21 connectport=10000 connectaddress=127.0.0.1", but now 127.65.43.21, or 127.65.43.21:8080 is not accessible by browser. I also tried 80 for listenport, and it's not working, neither. Any Idea? Thanks! 👍Krugersdorp
Hi @SiavashMortazavi : I would recommend you first verify that 127.0.0.1:10000 is accessible by Browser. Next Check that IPv4 and IPv6 Libraries are installed netsh interface ipv4 install and netsh interface ipv6 installClub
B
231

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

Bartlett answered 28/12, 2011 at 7:15 Comment(3)
how to use bookmarks , can you lead us to a link or some documentation or tutorials about thisSpanos
@Jereme: you should tell that to the OP, I can't do anything about itBartlett
@Bartlett 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!Beguile
D
194

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.

Disappointment answered 5/2, 2014 at 18:55 Comment(11)
This is a bad solution. You need to have Fiddler running at all times.Precipitate
@JamesHuckabone: Do you have a better solution? we'd love that!Disappointment
I get Error 400 Bad Request when i use this methodAlcoholism
Thanks for the solution, it works. but how do i configure it to support sub domainsPinckney
this no longer seems to work. Tried it with both fiddler2 and fiddler4Lorenelorens
@Lorenelorens 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.Orinasal
@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.Lorenelorens
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.Referendum
Bad Request - Invalid Hostname HTTP Error 400. The request hostname is invalid. This answer worked for me: https://mcmap.net/q/99452/-using-port-number-in-windows-host-file-closedAppellee
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.Vocable
its not a solution , just a hack or trick and worst part is it needs an additional tool/softwareHirst
H
17

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";
    oSession.hostname="mysite.com";
}
Hustings answered 13/6, 2015 at 10:58 Comment(1)
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.Orinasal
B
6

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.

Bosquet answered 13/12, 2020 at 23:16 Comment(2)
This is easily the simplest answer here! ThanksFragment
Unfortunately there are cases where putting the port in the browser isn't enough. In my case, part of my site's code is generated somewhere else and all the links don't include the port number. The completed site will be fine since it will run on port 80, but until then I want the links to work at port 8080.Interplead
B
5

-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

Bullard answered 29/7, 2017 at 3:26 Comment(0)
W
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

Weathercock answered 29/7, 2015 at 20:17 Comment(0)
S
1

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.

Stoplight answered 14/1, 2018 at 14:12 Comment(1)
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.Honky
X
1

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.

Xmas answered 8/2, 2018 at 18:34 Comment(0)
F
0

Alternate way

  1. Install Redirector
  2. Click Edit redirects -> Create New Redirect

enter image description here

Flexor answered 2/2, 2020 at 9:29 Comment(1)
If you dont understand the question. Dont try to answer it.Blockus
S
0

Well, I don't know specifically about wampserver or whatever, but I had a program running on http://127.0.0.1:5000/ and I wanted to change the domain name

so I inputted 127.0.0.1 myhost.local www.myhost.local in the host file and went to myhost.local:5000 and it showed the thing it was meant to!

Suffragette answered 18/4, 2023 at 3:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.