尝试通过 IP 范围将 URL 重定向到另一个页面

尝试通过 IP 范围将 URL 重定向到另一个页面

我这里有点麻烦。我有几条重写规则,但我认为它们不起作用。

我的主要目的是限制页面并只允许特定的 IP 或网络块。

RewriteEngine On
RewriteLog "/data/wre/var/logs/modrewrite.log"
RewriteLogLevel 5

RewriteCond %{REMOTE_ADDR} !^192\.168\.10\..*
RewriteCond %{REMOTE_ADDR} !^72\.139\.201\..*
RewriteCond %{REMOTE_ADDR} !^129\.233\.4\..*
RewriteCond %{REMOTE_ADDR} !^208\.118\.97\32
RewriteRule ^/solutions https://example.com/account/signin?go=outside [R,NE,NC]

我再次测试了它,但似乎不起作用?我做错了什么吗?

答案1

尝试这个:

RewriteEngine On
RewriteLog "/data/wre/var/logs/modrewrite.log"
RewriteLogLevel 5

RewriteCond %{REMOTE_ADDR} ^192\.168\.10\..* [OR]
RewriteCond %{REMOTE_ADDR} ^72\.139\.201\..* [OR]
RewriteCond %{REMOTE_ADDR} ^129\.233\.4\..*  [OR]
RewriteCond %{REMOTE_ADDR} ^208\.118\.97\.32
RewriteRule ^/solutions https://example.com/account/signin?go=outside [NE,NC,R=301]

请注意,多个 RewriteCond 指令是隐式 AND 运算,您需要指定是否应将它们改为 OR 运算。如果您希望浏览器记住重定向,则指定永久重定向可能会节省一些将来的处理时间。

根据您的配置,指定已知良好范围并重定向其他所有人可能会更容易。来自重定向外国人的重写手册:Apache 手册

RewriteEngine on
RewriteCond   %{REMOTE_HOST}  !^.+\.ourdomain\.com$
RewriteRule   ^/solutions      https://example.com/account/signin?go=outside [NE,NC,R=301]

答案2

看起来您正在创建一个窗帘(登录墙、付费墙或维护窗帘等),互联网上除了少数 IP 范围之外的所有 IP 范围都可以访问。

这是我为创建这种配置所做的工作:

# This implements a maintenance curtain; only these three IPs
# can look behind the curtain. Note its useful to allow the box
# itself too.
#
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !127.0.0.1
RewriteCond %{REMOTE_ADDR} !123.234.45.6
RewriteCond %{REMOTE_ADDR} !123.234.45.78
RewriteCond %{REQUEST_URI} !^/maintenance-curtain/.*
RewriteRule / / [L,R=503]

ErrorDocument 503 /maintenance-curtain/index.html
ProxyPass /maintenance-curtain !
Alias /maintenance-curtain /var/www/maintenance-curtain

这看起来确实和你的很相似。我建议你先简化一下,只用一个 IP 进行测试。如果其余配置相当复杂,可能会造成阻碍,因此请尝试在较小的环境中证明这一点,或者将其移到配置的早期。虚拟主机是否造成了阻碍;你是否需要复制配置(Include如果需要,请使用)。

不过我要说的是,我(我认为)知道在某些其他模块未启用的情况下不要设置 REMOTE_ADDR 之类的东西......自从我遇到这种行为已经有一段时间了...... cgi_module 是否启用了?

答案3

Prix​​,我实际上正在关注这一点。

http://www.the-art-of-web.com/system/rewrite/

Conversely, you may want to allow only certain IP addresses to access the site:

RewriteCond %{REMOTE_ADDR} !^12\.34\.56\.78$
RewriteCond %{REMOTE_ADDR} !^87\.65\.43\.21$
RewriteCond %{REQUEST_URI} !^sorry\.html  
RewriteRule .* /sorry.html 
Translation:

IF their IP address is not 12.34.56.78 
AND their IP address is not 87.65.43.21 
AND the request is not for sorry.html 
THEN display the sorry.html page 

答案4

也许我误解了你想要做什么,但如果我确实理解了你想要做什么,看起来你正在以一种非常复杂的方式做着事。

如果它适用于整个站点或整个目录,为什么不根据需要在虚拟主机节的目录中使用允许指令?

http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#allow

并将403错误页面设置为sorry.html http://httpd.apache.org/docs/2.2/mod/core.html#errordocument

因此类似于:

<directory "/path/to/content/" >

Order Allow,Deny
Allow 192.168.0.0/255.255.0.0  # allow everyone from the 192.168.0.0/255.255.0.0 network
# every one else gets denied (default for stuff that has not matched is to the 
# last clause of the Order directive
# the error document for a 403 (forbidden) is the sorry.html page
ErrorDocument 403  /sorry.html

</directory> 

相关内容