Possible Duplicate:
How to use DNS to redirect domain to specific port on my server

I want to trick my browser into going to localhost:3000 instead of xyz.com. I went into /etc/hosts on OS X 10.5 and added the following entry: xyz.com

That does not work but without specifying the port the trick works. Is there a way to do this specifying the port?


10 Answers 10


No, the hosts file is simply a way to statically resolve names when no DNS server is present.

  • 1
    is there any work-around?
    – Tony
    Aug 14, 2009 at 18:29
  • 11
    That's not completely accurate. If the machine is configured to use hosts before DNS then a hosts entry will be used even if there is a DNS entry for the same destination. Aug 14, 2009 at 21:35
  • 2
    Holy crap I did not know that. (later) @John Gardeniers Holy crap I did not know that either. Oct 4, 2012 at 20:28
  • 6
    I was able to get this working using the ifconfig and pfctl commands on Mac 10.10.2. With the following approach I'm successfully mapping to mydomain.com locally on my machine. I couldn't post this here, but I was able to post a step-by-step on how to do it here: serverfault.com/questions/102416/… Mar 6, 2015 at 17:25

The hosts file is for DNS resolution. DNS resolve names to IP addresses, and has nothing to do with ports I am afraid. You will need to use something else in conjunction with the hosts file to redirect the port (Mangle the TCP header by altering the destination port).

With iptables:
Does MAC OS use iptables / netfilter (I didn't think it did)..? If OS X uses iptables you could point xyz.com to some ip in the hosts file like and then:

sudo iptables -t nat -A OUTPUT -p tcp --dport 80 -d -j DNAT --to-destination


  • 4
    OS X descends from FreeBSD, so it uses ipfw and natd.
    – outis
    Aug 22, 2009 at 19:42
  • If I could upvote this 1000 times I would. Using the arbitrary port is genius. Thanks - works perfectly. Jan 13, 2010 at 0:56
  • 13
    The ifpw (mac friendly) way is sudo ipfw add 100 fwd,3000 tcp from any to any 80 in and to remove it sudo ipfw flush works like a charm! Jan 6, 2013 at 17:18
  • Why isn't this accepted answer? This should have been accepted.
    – dhilipsiva
    Jul 23, 2013 at 12:55
  • 3
    I get iptables command not found in Mavericks. Dec 5, 2013 at 18:08

You don't need to specify a port in the hosts file. just make the entry like you did omitting the port, as in xyz.com, this will direct you to your local host, then simply add port 3000 to the end of your URL... http://xyz.com:3000

  • A simple solution as compared to modifying IP tables. Helps for development where sessions require the domain name to be specified. Whereas IP tables may help with production server.
    – nightgaunt
    Sep 3, 2014 at 6:13
  • 9
    This is not what OP asked for.
    – Niels Bom
    Sep 23, 2014 at 10:24
  • This worked (Y)
    – Rider
    Oct 26, 2015 at 10:42
  • 15
    Q: "I want to do this" A: "No you don't."
    – Adam Grant
    Dec 2, 2016 at 1:12

Assuming you're trying to intercept http and not https, you'd have to be listening on port 80 on your local machine, but then you might be able to use ssh's port forwarding features by ssh'ing to localhost with -L80:localhost:3000, but you'll have to do that as root.

Would probably be better to have whatever it is that is running on port 3000 just listen to port 80 instead.

If you control the router between you and xyz.com, you might be able to setup a port forwarding rule instead.


The DNS solution for this is to use SRV records:


These are a way to allow DNS, which was originally a "name to number" or "number to name" distributed database to include "name to service endpoint", which could (optionally) include a protocol and port.

The bad news is that applications have to be developed to use SRV records, so it's not a drop-in solution for what you're trying to do.


I think you need to use some kind of proxy server or maybe something with firewall software to redirect port connections...


Chief-AG is right in that the hosts file is used to statically resolve names (DNS presence is irrelevant). However, there may be a combination of things you could do.

  1. Set the record in the hosts file to xyz.com
  2. Configure your machine for virtual hosts.
  3. For the virtual host you setup for xyz.com, create an html file that redirects to localhost:3000

Seems to be a fair bit of work, but it would accomplish what you're asking.


As an alternative to ;'s virtual hosts, you could create an ssh tunnel listening on port 80 and forwarding to localhost:3000.


i'm assuming this is for rails development? if so, then run script/server -p 80 to make it run on the standard web port. then your xyz.com will work


If you are using Apache as a webserver on xyz.com, you could use Apache's ProxyPass to 'convert' to to a different port.

Not the answer you're looking for? Browse other questions tagged .