Implementing a Captive portal using Apache

Rui*_*iro 5 apache-httpd

I have been trying to build a captive portal in Apache that I plan to be Apple CNA aware.

I found several relevant posts in StackOverflow, including Captive portal popups... and How to create WiFi popup login page.

我将相关的 Apache 配置定义为:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^CaptiveNetworkSupport(.*)$ [NC]
RewriteRule ^(.*)$ http://192.168.2.1/captive/portal.html [L,R=302]

# android
RedirectMatch 302 /generate_204 http://192.168.2.1/captive/portal.html

# windows
RedirectMatch 302 /ncsi.txt http://192.168.2.1/captive/portal.html
Run Code Online (Sandbox Code Playgroud)

它工作不完全正确,因为 CNA 浏览器进入重定向循环。

我还尝试将所有相关页面放入 /captive 目录,并定义规则

RewriteRule !^captive($|/) http://192.168.2.1/captive/portal.html [L,R=302]
Run Code Online (Sandbox Code Playgroud)

但有类似的循环问题。该怎么办?

Rui*_*iro 5

经过调查和一些测试,很明显 Apple CNA 是一个自己的网络浏览器;显然,如果没有正确地进行异常处理,所有后续请求将再次具有相同的用户代理。所以它将从头开始程序/门户重定向,从而重定向循环。

因此,在 Apple 的规则中,如果目标主机是强制门户服务器,我们将不再重定向。

# apple
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^CaptiveNetworkSupport(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^192.168.2.1$
RewriteRule ^(.*)$ http://192.168.2.1/captive/portal.html [L,R=302]

# android
RedirectMatch 302 /generate_204 http://192.168.2.1/captive/portal.html

# windows
RedirectMatch 302 /ncsi.txt http://192.168.2.1/captive/portal.html
Run Code Online (Sandbox Code Playgroud)

我们还在这里添加了一个通用的包罗万象的规则,如果前面的条件都没有发生,或者我们正在处理一个我们没有规则的操作系统,它会重定向到门户,如果不存在(例如不访问俘虏目录)。

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/captive/ [NC]
RewriteRule ^(.*)$ http://192.168.2.1/captive/portal.html [L]
Run Code Online (Sandbox Code Playgroud)

显然,我要强调的是,使用此配置,所有强制门户特定文件都必须位于 /captive 目录下。

另请参阅强制门户检测、弹出实现?