Apache-2.2

在 %{REMOTE_ADDR} 上匹配時,RewriteCond 在我的 IP 上不匹配

  • April 8, 2014

當流量從內部網路訪問 Web 伺服器時,我希望不執行 mod_rewrite 規則。Web 伺服器是 apache 2.2 以下 RewriteCond 旨在保護重寫規則。

       RewriteCond %{REMOTE_ADDR} !=192\.168\.[0-15]\.[1-255]

如果我使用 ip 192.168.15.173 訪問 Web 伺服器,則該規則似乎沒有啟動,因此儘管我的內部地址仍然執行重寫規則。我的錯誤在哪裡?

匹配更簡單的:

   RewriteCond %{REMOTE_ADDR} !=192\.168\.15\.173

也失敗了。

我使用http://httpd.apache.org/docs/trunk/rewrite/access.html中的“阻止機器人”範例來建構規則。我錯過了什麼嗎?

編輯:我已經嘗試使用重寫日誌進行調查,但這並沒有帶來任何有用的資訊。這就是請求期間發生的情況:

   192.168.15.173 - - [12/Jun/2013:13:50:17 +0200] [example.com/sid#7f3c6afb5e30][rid#7f3c6f864b68/initial] (2) init rewrite engine with requested uri /test/
   192.168.15.173 - - [12/Jun/2013:13:50:17 +0200] [example.com/sid#7f3c6afb5e30][rid#7f3c6f864b68/initial] (3) applying pattern '^/test/(.*)' to uri '/test/'
   192.168.15.173 - - [12/Jun/2013:13:50:17 +0200] [example.com/sid#7f3c6afb5e30][rid#7f3c6f864b68/initial] (2) rewrite '/test/' -> 'https://example2.com/'
   192.168.15.173 - - [12/Jun/2013:13:50:17 +0200] [example.com/sid#7f3c6afb5e30][rid#7f3c6f864b68/initial] (2) explicitly forcing redirect with https://example2.com/
   192.168.15.173 - - [12/Jun/2013:13:50:17 +0200] [example.com/sid#7f3c6afb5e30][rid#7f3c6f864b68/initial] (1) escaping https://example2.com/ for redirect
   192.168.15.173 - - [12/Jun/2013:13:50:17 +0200] [example.com/sid#7f3c6afb5e30][rid#7f3c6f864b68/initial] (1) redirect to https://example2.com/ [REDIRECT/302]

我什至沒有看到此日誌中提到的 RewriteCondition。

Edit2:詳細的apache版本是:

Name        : httpd
Arch        : x86_64
Version     : 2.2.15
Release     : 26.el6.centos
Size        : 2.9 M
Repo        : installed
From repo   : anaconda-CentOS-201303020151.x86_64

Edit3:我將 LogLevel 提高到 4,現在我得到了更多結果:

192.168.15.173 - - [12/Jun/2013:14:18:05 +0200] [example.com/sid#7f3c6afdfdb0][rid#7f3c6f880568/initial] (4) RewriteCond: input='192.168.15.173' pattern='!=192\.168\.15\.173' => matched

為什麼會這樣匹配?

Edit4:http: //regexr.com?356tm告訴我,它不應該匹配……

我找到了我的問題的答案。正確的條件應該是:

RewriteCond %{REMOTE_ADDR} !192\.168\.[0-9]+\.[0-9]+

引用自:https://serverfault.com/questions/515222