お問合せ頂いた質問に答えてみます。MovableType では,タグを指定して記事を検索することができますが,その場合,通常では URL は以下のような長いものになります。
http://www.example.com/cgi-bin/mt/mt-search.cgi?IncludeBlogs=1&Template=magicvox&tag=Hogehoge
これを以下のようなスマートな URL でアクセスできるようにする方法を紹介します。こちらの方がスッキリと記述できますし,SEO 効果も期待できそうです。
http://www.example.com/archive/tag/Hogehoge
これを実現するには,Web サーバに mod_rewrite モジュールがインストールされている必要があります。設置方法や具体的な記述方法について詳しくはサーバ管理者に問い合わせてください。このサイト(さくらインターネット)では,.htaccess に以下のような記述をすることで,タグ検索を静的 URL のように見せかけています。
記述を具体的に見てみます。1 行目では Apache の mod_rewrite モジュールによる URL の書き換え機能を有効にすることを指示しています。そして 3 行目では,URL のパス部分が /archive/tag/ から始まる場合に,2 行目で指定されたパスが実在するファイルでは無いことをチェック(!-f)し,最後に実際の処理を行う CGI にタグ名($1)を渡しています。
ルールとしては非常に単純なものですが,mod_rewrite を使った URL 書き換えは様々な場面で活用できるので,覚えておいて損はないでしょう。
寄せられたコメント (全 5 件中、最新 5 件まで表示しています)
よろしくおねがいします。
ぴろりさま
いつぞやはありがとうございました。
mod_rewrite を使いたいのですが、上記のコードにおいて、
Template=magicvox
とは、例えばタグ・クラウドを表示するためのインデックス・テンプレートの
名前であってもよいのでしょうか?
Dear UENISHI,
Thanks for you kindly advice first.
I have a test per your guide line but failed, same error "Invalid request: corrupt character data for character set UTF-8". I think there is something wrong when passing the charset code from the link http://www.abc.com/tag/汉字 to http://www.abc.com/cgi-bin/mt/mt-search.cgi&tag=汉字, because there is no problem in this scheme "http://www.abc.com/tag/english". Do you think is the writing of .htaccess?
My .htaccess config:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule archives/tags/(.+)$ /cgi-bin/mt/mt-search.cgi?&tag=$1&blog_id=1
Thanks again.
Hi, Tony. Thank you for your comment!
If your blog is written in UTF-8 charset, try 'encode_url' modifier for retrieving the tag name in a link. like;
<a href="/archives/tags/<$mt:TagName encode_url="1"$>"><$mt:TagName$></a>
Hell UENISHI ,
I have try the rewrite function for my MT site per your instruction, below is my rules in .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule archives/tags/(.+)$ /cgi-bin/mt/mt-search.cgi?IncludeBlogs=1&tag=$1
RewriteRule archives/keywords/(.+)$ /cgi-bin/mt/mt-search.cgi?IncludeBlogs=1&search=$1
There is no problem when my tag name is write in English, but I have problem when the MT handle the TAG write in the Chinese, it give me an error "Invalid request: corrupt character data for character set UTF-8". I have test the search template, the charset is in UTF8. I think the Japanese is fimilar with Chinese, can you tell me how you can performance in your site? (like: http://www.magicvox.net/archive/keyword/%E3%82%B5%E3%82%A4%E3%83%88%E6%A7%8B%E7%AF%89)
Thanks and best regards,
Tony