PC 向けのページを携帯でも見られるようにする(Google Wireless Transcoder 編)
先日紹介した mod_chxj よりもはるかにお手軽に PC 向けのページを携帯でも見られるようにする方法があります。Google Wireless Transcoder を使う方法です。ただし、この場合 Google にすべての情報を握られてしまいますので(通常はログインしないと見られないページの中身も全てです)、それを許容できる場合だけ有効な方法です。
その方法とは http://www.google.co.jp/gwt/n?u= のあとにURLエンコードした任意のURLをつけてやるだけです。サイトの入り口に以下のような CGI を置いておけば、User-Agent を見てそのまま表示するか(index.html)、Google Wireless Transcoder を使うか区別できます。
index.c
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char** argv) { char *agent = getenv("HTTP_USER_AGENT"); // エージェント不明 if (agent == NULL) { printf("Location: ./index.html\n\n"); } else { // 携帯(imode) if (strstr(agent, "DoCoMo") != NULL) { printf("Location: http://www.google.co.jp/gwt/n?u=http%%3A%%2F%%2Fwww.example.co.jp\n\n"); // 携帯(j-phone / vodafone / softbank) } else if (strstr(agent, "J-PHONE") != NULL || strstr(agent, "Vodafone") != NULL || strstr(agent, "SoftBank") != NULL) { printf("Location: http://www.google.co.jp/gwt/n?u=http%%3A%%2F%%2Fwww.example.co.jp\n\n"); // 携帯(ezweb) } else if (strstr(agent, "UP.Browser") != NULL) { printf("Location: http://www.google.co.jp/gwt/n?u=http%%3A%%2F%%2Fwww.example.co.jp\n\n"); // PC } else { printf("Location: ./index.html\n\n"); } } return 0; }
以下のようにしてコンパイルした index.cgi をサイトのトップページと同じ場所に置きます。Apache の設定をいじって index.html より index.cgi のほうが先に参照されるようにしておきます。
$ gcc -O2 -o index.cgi index.c $ strip index.cgi