Redirect all traffic with iptables

This little trick can be very useful when you do a migration from a server to another one (during DNS propagation for instance) :

Note: the following commands have to be run as root

Step 1 - Enable IP Forwarding :

~$ echo 1 >/proc/sys/net/ipv4/ip_forward

Step 2 - Redirect everything from port 80 to the new server :

# redirection
~$ iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination [NEW_SERVER_IP]
# IP Masquerading
~$ iptables -t nat -A POSTROUTING -p tcp -d [NEW_SERVER_IP] --dport 80 -j MASQUERADE

source :