Apache-Httpd

在 Apache 上禁止來自某個網路的某些 IP?

  • August 16, 2016

我想將所有內部網路 IP(並且只有內部網路“192.168.1.0”)重定向到除某些 IP 之外的錯誤頁面,條件如下:

if ( IP_from_Network = 192.168.1.0 and ((IP != 192.168.1.4) or (IP != 192.168.1.5)
or (IP != 192.168.1.6)) )
{
redirect to an error page
}

所以我試圖使用 RewriteEngine 來實現這一點:

RewiteEngine On
RewriteCond   %{REMOTE_ADDR}   !^192\.168\.1\.4$  [NC]
RewriteCond   %{REMOTE_ADDR}   !^192\.168\.1\.5$  [NC]
RewriteCond   %{REMOTE_ADDR}   !^192\.168\.1\.6$  [NC]
RewriteCond   %{REMOTE_ADDR}   ^192\.168\.1\.*$  [NC]
RewriteCond   %{REQUEST_URI}   ^/test/manager/.* [NC]
RewriteRule    ^(.*)$           -                 [R=404,L]

但這對我不起作用

我應該使用其他標籤嗎

$$ OR $$或者$$ AND $$? 更新:

目錄標籤:

<Directory /var/www/html/test>
 Order allow,deny
 Allow from 192.168.1
 RewriteEngine on
 RewriteCond   %{REMOTE_ADDR}   !^192\.168\.1\.4$  [NC]
 RewriteCond   %{REMOTE_ADDR}   !^192\.168\.1\.5$  [NC]
 RewriteCond   %{REMOTE_ADDR}   !^192\.168\.1\.6$  [NC]
 RewriteCond   %{REMOTE_ADDR}   ^192\.168\.1\.*$  [NC]
 RewriteCond   %{REQUEST_URI}   ^/test/manager/.* [NC]
 RewriteRule   ^(.*)$           -                 [R=404,L]
</Directory>

改用允許/拒絕

<Location /test/manager/>
 Order Deny,Allow
 Deny from  192.168.1.0/24
 Allow from 192.168.1.4 192.168.1.5 192.168.1.6
</Location>

請注意,這也允許任何其他 IP,我認為這不是您想要的。如果是這樣,交換訂單並刪除拒絕行:

<Location /test/manager/>
 Order Allow,Deny
 Allow from 192.168.1.4 192.168.1.5 192.168.1.6
</Location>

引用自:https://unix.stackexchange.com/questions/148905