前回紹介したMovabletypeで携帯サイト作成で紹介した方法で、携帯向けサイトを公開した際の、PCページから対応する携帯ページへリダイレクトする方法を、紹介します。
●準備
PCサイトの.htmlファイルで、phpが動くようにしておきます。
PCサイト用.htaccessファイルに、次のような記述をしておいてください。
AddType application/x-httpd-php .php .html
●簡易的な方法
PCサイト用のテンプレートに、次のようなPHPスクリプトを先頭に埋め込みます。
USER_AGENTの値を調べて、携帯特有の文字列があれば、携帯電話からのアクセスと見なして、リダイレクトする仕組みになっています。
大体の機種はこれでリダイレクトできますが、取りこぼしがあるかもしれません。
メインページ(index.html)
<?php
$url="<$MTBlogURL$>m/";
$seiki='/^DoCoMo|^KDDI|^DDIPOKET|^UP\.Browser|^J-PHONE|^Vodafone|^SoftBank/';
if(preg_match($seiki,$_SERVER['HTTP_USER_AGENT'])>0){
header("location: $url");
}
?>
カテゴリー・アーカイブ:テンプレート
<?php $url="<$MTBlogURL$>m/cat_エントリー・アーカイブ:テンプレート.html"; $seiki='/^DoCoMo|^KDDI|^DDIPOKET|^UP\.Browser|^J-PHONE|^Vodafone|^SoftBank/'; if(preg_match($seiki,$_SERVER['HTTP_USER_AGENT'])>0){ header("location: $url"); } ?> <$MTCategoryID$>
<?php
$url="<$MTBlogURL$>m/<$MTEntryID$>.html";
$seiki='/^DoCoMo|^KDDI|^DDIPOKET|^UP\.Browser|^J-PHONE|^Vodafone|^SoftBank/';
if(preg_match($seiki,$_SERVER['HTTP_USER_AGENT'])>0){
header("location: $url");
}
?>
日付アーカイブ:テンプレート
<?php $url="<$MTBlogURL$>m/<$MTEntryDate format="%Y_%m"$> .html"; $seiki='/^DoCoMo|^KDDI|^DDIPOKET|^UP\.Browser|^J-PHONE|^Vodafone|^SoftBank/'; if(preg_match($seiki,$_SERVER['HTTP_USER_AGENT'])>0){ header("location: $url"); } ?>
●Net_UserAgent_Mobileを利用する方法
PEARのNet_UserAgent_Mobileを利用して、機種を判別します。
<?php
require_once('Net/UserAgent/Mobile.php');
$agent=Net_UserAgent_Mobile::singleton();
if($agent->isDoCoMo() or $agent->isEZweb() or $agent->isSoftBank()){
$url="<$MTBlogURL$>m/";
header("location: $url");
}
?>
カテゴリー・アーカイブ:テンプレート
<?php
require_once('Net/UserAgent/Mobile.php');
$agent=Net_UserAgent_Mobile::singleton();
if($agent->isDoCoMo() or $agent->isEZweb() or $agent->isSoftBank()){
$url="<$MTBlogURL$>m/cat_<$MTCategoryID$> .html";
header("location: $url");
}
?>
エントリー・アーカイブ:テンプレート
<?php
require_once('Net/UserAgent/Mobile.php');
$agent=Net_UserAgent_Mobile::singleton();
if($agent->isDoCoMo() or $agent->isEZweb() or $agent->isSoftBank()){
$url="<$MTBlogURL$>m/<$MTEntryID$>.html";
header("location: $url");
}
?>
日付アーカイブ:テンプレート
<?php
require_once('Net/UserAgent/Mobile.php');
$agent=Net_UserAgent_Mobile::singleton();
if($agent->isDoCoMo() or $agent->isEZweb() or $agent->isSoftBank()){
$url="<$MTBlogURL$>m/<$MTEntryDate format="%Y_%m"$> .html";
header("location: $url");
}
?>
●おまけ
Movabletypeの設定に詳しい人であれば、携帯サイトのマッピング設定をPCサイトと同じ(サイト構造が同じ)にすることにより、各ページへのパス、ファイル名を全く同じにすることができる。
その場合、.htaccessファイルで、
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^(DoCoMo|KDDI|DDIPOKET|UP\.Browser|J-PHONE|Vodafone|SoftBank)
RewriteRule ^(.*)$ /m/$1 [R]
のように設定すれば、.htmlファイルで、phpを動かす準備をする必要はなくなる。
●カテゴリ