Mod_Rewrite もどき
2006年3月27日
現在使用中のプロバイダ、nifty のラクーカンでは、Mod_Rewrite が使用できない。メールで問い合わせてみたが、やはり駄目のようである。
以前使っていたサーバでは、Mod_Rewrite を多用していただけにつらいところである。何か手はないかと探ってみたら、完全ではないものの、一つ解決方法を見つけた。『Redirect』『FilesMatch』なら使えるようなので、これを使用した。
まず、.htaccess に次のように記述
さらに、以下のようなphp (ファイル名:pseudo.php)を用意した。
これで、mod_rewriteもどきのブログのできあがり。普通の mod_rerite と違う点は、たとえば 『/nucleus/item-143.html』 にアクセスすると、実際には『/nucleus/pseudo.php?/item-143.html』が表示されることである。サーバとブラウザの間で一度余分な通信が入るが、keep-alive で通信しているならば、速度の問題はほとんどなさそうである。
以前使っていたサーバでは、Mod_Rewrite を多用していただけにつらいところである。何か手はないかと探ってみたら、完全ではないものの、一つ解決方法を見つけた。『Redirect』『FilesMatch』なら使えるようなので、これを使用した。
まず、.htaccess に次のように記述
<FilesMatch "^(item|catid|archive|archivelist)\-([0-9\-]+)\.html$"> Redirect /nucleus/ /nucleus/pseudo.php? </FilesMatch>
さらに、以下のようなphp (ファイル名:pseudo.php)を用意した。
<?php // This file will generate and return the main page of the site $CONF = array(); $CONF['Self'] = 'index.php'; $_GET=array(); if ($i=strpos($_SERVER['REQUEST_URI'],'?/')) { $page=substr($_SERVER['REQUEST_URI'],$i+2); if (preg_match('/^archive\-([0-9]+)\-([0-9]+)\-([0-9]+)\.html$/',$page,$matches)) { $_GET['archive']=$matches[2].'-'.$matches[3]; $_GET['blogid']=$matches[1]; } else if (preg_match('/^item\-([0-9]+)\.html$/',$page,$matches)) { $_GET['itemid']=$matches[1]; } else if (preg_match('/^catid\-([0-9]+)\.html$/',$page,$matches)) { $_GET['catid']=$matches[1]; } else if (preg_match('/^archivelist\-([0-9]+)\.html$/',$page,$matches)) { $_GET['archivelist']=$matches[1]; } else if (preg_match('/\?([^\?]+)$/',$page,$matches)) { foreach(explode('&',$matches[1]) as $value) if ($i=strpos($value,'=')) { $_GET[substr($value,0,$i)]=urldecode(substr($value,$i+1)); } } } else if (preg_match('/\?([^\?]+)$/',$page,$matches)) { foreach(explode('&',$matches[1]) as $value) if ($i=strpos($value,'=')) { $_GET[substr($value,0,$i)]=urldecode(substr($value,$i+1)); } } include('./config.php'); selector(); ?>
これで、mod_rewriteもどきのブログのできあがり。普通の mod_rerite と違う点は、たとえば 『/nucleus/item-143.html』 にアクセスすると、実際には『/nucleus/pseudo.php?/item-143.html』が表示されることである。サーバとブラウザの間で一度余分な通信が入るが、keep-alive で通信しているならば、速度の問題はほとんどなさそうである。