Sometimes we have to map a hostname to loopback address 127.0.0.1 by some reasons of development. This is a simple guide to show how to do that on MacOS 10.10 Yosemite.
Everythings starts with hosts file
Add the following line in
If your local port goes with
80, this is all that you need to do.
But mostly we start a develop web server with
Non-80 port such as
localhost:8080. So port forwarding comes.
First, add an anchor file
/etc/pf.anchors/com.custom to contain the
port forwarding rule.
rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port 80 -> 127.0.0.1 port 8080
Then, add two lines to /etc/pf.conf to load your new rule. It is important where these lines go. Add this line right after rdr-anchor "com.apple/*":
Add this line directly after
load anchor "com.apple" from `/etc/pf.anchors/com.apple":
load anchor "pow" from "/etc/pf.anchors/com.custom"
Next, reload the rules into pf by running
sudo pfctl -f /etc/pf.conf
pf by running
sudo pfctl -e
test.domain.com into your browser address bar, it will works.