You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何检测VPN/代理并将用户重定向至custom.html页面

问题描述
  • 需求:当访问者使用VPN/代理时,自动重定向至custom.html页面,告知其当前使用了VPN/代理,目的是迫使恶意用户使用真实IP以便封禁(网站在线聊天仅支持IP封禁,存在恶意用户每日用不同VPN/IP骚扰)
  • 现状:已添加的.htaccess代码无法有效拦截VPN/代理访问,寻求修复方案
现有.htaccess代码
RewriteOptions inherit
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^products\/product\-01\/category\-id\-001\-abc$ "https\:\/\/example\.com\/product\-01\-category\.html" [R=301,L]

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

RewriteCond %{HTTP:VIA}                 !^$ [OR]
RewriteCond %{HTTP:FORWARDED}           !^$ [OR]
RewriteCond %{HTTP:USERAGENT_VIA}       !^$ [OR]
RewriteCond %{HTTP:X_FORWARDED_FOR}     !^$ [OR]
RewriteCond %{HTTP:PROXY_CONNECTION}    !^$ [OR]
RewriteCond %{HTTP:XPROXY_CONNECTION}   !^$ [OR]
RewriteCond %{HTTP:HTTP_PC_REMOTE_ADDR} !^$ [OR]
RewriteCond %{HTTP:HTTP_CLIENT_IP}      !^$
RewriteRule ^(.*)$ - [F]

RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^146\.70\.
RewriteRule ^ - [F]

Order Allow,Deny
Deny from 85.31.0.
Deny from 146.70.46.
Deny from 107.181.177.
Deny from 122.161.53.
Deny from wiredns.net
Deny from datapacket.com
Deny from 154.47.30.
Deny from 137.59.145.
DEny from 89.46.62.
Deny from lightspeed.wepbfl.sbcglobal.net
Allow from all
解决方案

1. 替换核心拦截规则(实现重定向)

原规则仅返回403禁止,未实现重定向,且检测条件不全。替换为以下规则,实现VPN/代理检测后跳转至custom.html:

# 检测VPN/代理并跳转至custom.html
RewriteEngine On
RewriteCond %{HTTP:VIA} !^$ [OR]
RewriteCond %{HTTP:FORWARDED} !^$ [OR]
RewriteCond %{HTTP:USERAGENT_VIA} !^$ [OR]
RewriteCond %{HTTP:X_FORWARDED_FOR} !^$ [OR]
RewriteCond %{HTTP:PROXY_CONNECTION} !^$ [OR]
RewriteCond %{HTTP:XPROXY_CONNECTION} !^$ [OR]
RewriteCond %{HTTP:HTTP_PC_REMOTE_ADDR} !^$ [OR]
RewriteCond %{HTTP:HTTP_CLIENT_IP} !^$ [OR]
RewriteCond %{HTTP:X-Forwarded-Host} !^$ [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !^$ [OR]
RewriteCond %{REMOTE_ADDR} ^(10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.|192\.168\.)
RewriteRule !^custom\.html$ /custom.html [R=302,L]
  • 说明:新增更多代理相关HTTP头检测,加入内网IP段检测(多数VPN会分配这类IP);排除custom.html本身,避免无限重定向;使用302临时重定向,便于后续调整。

2. 调整规则执行顺序

确保代理检测规则放在HTTPS强制跳转、域名/URL重定向规则之后,PHP handler和手动IP封禁规则之前,正确顺序如下:

  1. HTTPS强制跳转规则
  2. 域名/URL重定向规则
  3. VPN/代理检测重定向规则
  4. PHP handler配置
  5. 手动IP封禁规则

3. 升级IP封禁语法(兼容Apache 2.4+)

原Order Allow,Deny为旧版语法,替换为新版Require语法:

# 替换原Order/Deny规则
<RequireAll>
    Require all granted
    Require not ip 85.31.0.0/16
    Require not ip 146.70.46.0/24
    Require not ip 107.181.177.0/24
    Require not ip 122.161.53.0/24
    Require not ip 154.47.30.0/24
    Require not ip 137.59.145.0/24
    Require not ip 89.46.62.0/24
    Require not host wiredns.net
    Require not host datapacket.com
    Require not host lightspeed.wepbfl.sbcglobal.net
</RequireAll>

4. 优化建议

  • 定期更新VPN/代理IP段:通过专业IP库获取最新VPN/代理地址,补充到拦截规则中
  • 在custom.html页面明确提示用户关闭VPN/代理后访问,引导恶意用户暴露真实IP
  • 测试验证:使用不同VPN/代理工具访问网站,确认是否正确跳转到目标页面

内容的提问来源于stack exchange,提问作者MiningChamber

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 18:01:30