I would like to direct all the traffic through my VPN connection on my Android based phone (HTC Desire). The VPN works great, but problem is that when the VPN disconnects, the traffic is redirected through my regular internet, something I don't want to happen. How can I block the traffic outside the VPN?
You could use DroidWall for this IF you're rooted. Just deny all traffic and then allow traffic to your VPN IP/port. If you need to use a custom script see the man page of iptables for the syntax. Here's a quick, untested example.
iptables -P FORWARD ACCEPT iptables -P INPUT DROP iptables -P OUTPUT DROP iptables -A OUTPUT -p udp --dport 1194 -d xxx.xxx.xxx.xxx -j ACCEPT
Previous answer is a bit outdated (but should still work), because DroidWall isn't developed anymore, while there's a fork named AFWall+. App has free and "donate" versions and lets you to allow apps access internet only through VPN using GUI. You'll probably have to obtain paid version for this functionality, but I'm not sure about that because description in F-Droid says you need to buy it, while in Play Store there's nothing about that. Root is needed of course.
On screenshot WireGuard is VPN app and is allowed on all networks, other apps are allowed only on VPN (fourth column), some are not allowed to access internet at all:
Also since Android 8 there's built-in option Block connections without VPN in VPN settings.