前提・実現したいこと
Apache httpd でクエリ文字列に remote_addr=xxxx が設定されていないときのみ、リダイレクトしたいが、正規表現の前に ! をつける方法でのマッチングがうまくいかない。
発生している問題・エラーメッセージ
curl で確認したところ、クエリ文字列に remote_addr=xxxx が設定されている場合でもリダイレクトされてしまう。
$ curl -v http://test.example.com/?remote_addr= * Trying 192.168.56.40... * TCP_NODELAY set * Connected to test.example.com (192.168.56.40) port 80 (#0) > GET /?remote_addr= HTTP/1.1 > Host: test.example.com > User-Agent: curl/7.54.0 > Accept: */* > < HTTP/1.1 302 Found < Date: Sat, 30 Jun 2018 00:50:33 GMT < Server: Apache/2.4.6 (CentOS) < Location: http://echoip.example.com/ < Content-Length: 214 < Content-Type: text/html; charset=iso-8859-1 < <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>302 Found</title> </head><body> <h1>Found</h1> <p>The document has moved <a href="http://echoip.example.com/">here</a>.</p> </body></html> * Connection #0 to host test.example.com left intact
該当のソースコード
以下の RewriteCond で クエリ文字列に remote_addr= が含まれていないときのみリダイレクトするように設定したつもりであるが、上記の通り、remote_addr= が含まれていてもリダイレクトされてしまう。
以下のコードを /etc/httpd/conf.d/test.conf として置いた。
<VirtualHost *:80> ServerName test.example.com RewriteEngine On RewriteCond %{QUERY_STRING} !^remote_addr= RewriteRule .* http://echoip.example.com/ </VirtualHost>
試したこと
上記の RewriteCond で ! を外すと、remote_addr= があるときだけリダイレクトされる。
6/30 追記
回答いただいた内容に従って、 ! を使わずに単にクエリ文字列が空であることを検出しようとしましたが、だめでした。
<VirtualHost *:80> ServerName test.example.com RewriteEngine On RewriteCond %{QUERY_STRING} ^$ RewriteRule .* http://echoip.example.com/ </VirtualHost>
これでもhttp://test.example.jp/?remote_addr
はリダイレクトされました。
補足情報(FW/ツールのバージョンなど)
Vagrant box の Cent OS に yum install httpdでインストールした apache でテストしました。
# httpd -v Server version: Apache/2.4.6 (CentOS) Server built: Apr 20 2018 18:10:38 # cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core)
端末(Mac OS)の /etc/hosts ファイルを書き換えて curl から名前でアクセスできるようにしてテストしました。