Auto-trigger captive portal agreement page display

Jason Burgett asked:

I have a WiFi hotspot setup using Hostapd and Dnsmasq. Everything works great, but I’m trying to trigger the sort of Terms and Conditions page you might see at a hotel or coffee shop after you connect to their captive portal. Most modern devices will automatically bring up this Terms and Conditions page immediately after connecting to one of these captive portal networks, which is what I’d like to happen after connecting to my hotspot.

As I understand it this is usually detected by the device by checking to see if a specific website returns a predictable response. For instance Google seems to test a connection to http://clients1.google.com and if it gets any response other than “generate204” it should trigger the captive portal agreement page. I’ve found two ways to redirect all traffic (and presumably these specific domains) to a web server running on my device…

One by adding the following line to /etc/dnsmasq.conf:

address=/#/10.0.0.1

and the other by using iptables. This triggers from a script on boot:

iptables -t nat -A PREROUTING -d 0/0 -p tcp --dport 80 -j DNAT --to-destination 10.0.0.1

Both succeed in redirecting all traffic to 10.0.0.1 (which is the correct address for the local web server) when manually entering a url into the browser, but the page doesn’t open automatically upon connecting to the access point.

I’ve also tried manually adding entries for specific google URL’s like:

address=/clients1.google.com/10.0.0.1

into the /etc/dnsmasq.conf with no luck. I’ve run out of ideas, any suggestions.

My answer:


To make your captive portal work, your captive portal web server, to which iptables is redirecting traffic, needs to send a 302 redirect back to the client, for any request which is not for your captive portal’s hostname. You can put that in the default virtual host, and serve your captive portal web page from a second virtual host.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.