リダイレクト回数をカウントする

Apachemod_rewrite

  • 携帯の場合は http にリダイレクトして、
  • PCの場合は https にリダイレクトして、

という、一見ループしかねない設定をしていた。

携帯の実機を使うとリダイレクトの回数制限が結構ひっかかってしまうのだ。
Apacheaccess_log や ssl_request.log をみて、回数をカウントするのは面倒なので作ってしまった。

#!/bin/bash

LANG=C
WGET_OPTS='--no-proxy --no-check-certificate --user-agent='

URLS="
http:// ... /
http:// ... / ... /
http:// ... / ... / ... /
https:// ... /
https:// ... / ... /
https:// ... / ... / ... /
"

AGENTS="
 Mozilla/4.0
 DoCoMo/1.0/N505i
"

now=`date '+%Y%m%d%H%M%S'`
temp_file=/tmp/wget-$$
out_file="./out.file.$now"

for agent in $AGENTS
do
 echo [$agent] | tee -a $out_file
 for url in $URLS
 do
   wget $WGET_OPTS$agent $url -o $temp_file
   c=`cat $temp_file | grep Location | wc -l`
   echo " - " $url [$c] | tee -a  $out_file
   sleep 1
 done
done

\rm -f index.*
\rm -f *session*

httpsオレオレ証明書なので、 --no-check-certificate オプションをつけている。

$ LANG=C wget --no-proxy --no-check-certificate --user-agent=DoCoMo/1.0/N505i https://127.0.0.1
--02:03:33--  https://127.0.0.1/
Connecting to 127.0.0.1:443... connected.
WARNING: cannot verify 127.0.0.1's certificate, issued by `/C=JP/ST=xxxxx/O=xxxxx/CN=xxx.xxx.xxx/emailAddress=xxx@xx.xx':
  Unable to locally verify the issuer's authority.
WARNING: certificate common name `xxx.xxx.xxx' doesn't match requested host name `127.0.0.1'.
HTTP request sent, awaiting response... 302 Found
Location: http://127.0.0.1/xxx/mobile [following]
--02:03:33--  http://127.0.0.1/xxx/mobile
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: http://127.0.0.1/xxx/mobile/ [following]
--02:03:33--  http://127.0.0.1/xxx/mobile/
Reusing existing connection to 127.0.0.1:80.
HTTP request sent, awaiting response... 200 OK
Length: 6 [text/plain]
Saving to: `index.html.1'

'Location'の回数をリダイレクト回数としてカウントしている。間違っているかもしれないが目安にはなる。

実際は、これで携帯PCそれぞれのコンテンツに遷移しているかのチェック(wget の -O オプションと grep でチェック)して、mod_rewrite の設定を変えては、このスクリプトを流してという感じで、そこそこいい設定まで落とし込むことができた。