Movable Type 備忘録

 

 MovableTypeで楽にリダイレクトする

サイト内のディレクトリ構成変更または、ディレクトリ名、ページ名を変更してしまうとURLが変わってしまい、 変更前のURLに、せっかく訪問者さんが来てくれようとしてもリンク切れになってしまうので、訪問者さんにとっても、自分にとってもいいことありませんね。

今回は、MovableType(ムーバブルタイプ)で楽にリダイレクトさせるカスタマイズについて紹介してみたいと思います。

- 2007.05.22 追記 -

記事内容をもっと分かりやすく書き直しました。

実は、わたしのサイトでも最近、ディレクトリ( カテゴリ )名の変更と、PHP化によるページファイル名が変更になってしまったので、今から紹介する方法で楽に移行が完了したんですよ(●´∀`●)

リダイレクトとは具体的にどんなものなのか、というと以下のURLをクリックしてみてくてださい。

http://bizcaz.com/archives/movabletype/template/archives/2007/02/05-011951.php

上記URLは、わたしのサイトで公開してましたページの変更前のURLなんです。
ですが、クリックした先はというと、

http://bizcaz.com/archives/2007/02/05-011951.php

というリンク先になっていますね ( ̄∇ ̄)b
これがリダイレクトという機能です。

変更前のURL「http://bizcaz.com/archives/movabletype/template/archives/2007/02/05-011951.php」は、実際には今は存在していません。

このURL「http://bizcaz.com/archives/movabletype/template/archives/2007/02/05-011951.php」にアクセスされると、自動的にhttp://bizcaz.com/archives/2007/02/05-011951.phpにリダイレクトされる…というしくみになっています。

カンタンですが、リダイレクトについて分かりましたか。
では、MovableType(ムーバブルタイプ)の機能によるリダイレクトを実現してみたいと思います。

今回は友人に教えてもらいましたぁ…THANKS!

.htaccessファイルを作るためのテンプレート追加

リダイレクトは以下のようなフォーマットになっています。

Redirect permanent  ウェブサイトのルートディレクトリからのパス  リダイレクト先のURL

青い字の部分は必ずルーティディレクトリからのパスを指定する必要があります。
URLではありませんのでお間違えないように

  1. まず、プラグインをインストールします。
    以下のMTRelativePathプラグインをインストールすることで、ウェブサイトのURLからルートディレクトリのパスに変換してくれます。
    MTRelativePath

    インストール先はいつものように、mt/plugins/ディレクトリの中にアップロードしてください。

  2. 次にリダイレクト用にインデックス・テンプレートを作成します。
    MovableType(ムーバブルタイプ)管理画面からブログを選択して、左サイドバーにある「テンプレート」をクリックします。
    そして、画面右上にある「テンプレートを新規作成」をクリックしてください。

    入力内容は以下の通りです。

    テンプレート名:htaccess
    出力ファイル名:htaccess.txt
    このテンプレートにリンクするファイル:未記入でいいです。
    テンプレートの内容:以下のコードをコピペします。
    
    # トップページをリダイレクトする場合
    Redirect permanent <$MTBlogURL relative_url="1"$>index.html <$MTBlogURL$>index.php
    # 各エントリをリダイレクトする場合
    <MTEntries lastn="0">
    Redirect permanent <$MTEntryPermalink relative_url="1"$> <$MTBlogArchiveURL$><MTEntryDate format="%Y/%m/%d-%H%M%S"$>.php
    </MTEntries>
    # 各カテゴリをリダイレクトする場合
    <MTArchiveList archive_type="Category">
    Redirect permanent <$MTArchiveLink relative_url="1"$> <$MTArchiveLink$>index.php
    </MTArchiveList>
    # 各月別ページをリダイレクトする場合
    <MTArchiveList archive_type="Monthly">
    Redirect permanent <$MTArchiveLink relative_url="1"$> <$MTArchiveLink$>index.php
    </MTArchiveList>
    # 各日別ページをリダイレクトする場合
    <MTArchiveList archive_type="Daily">
    Redirect permanent <$MTArchiveLink relative_url="1"$> <$MTArchiveLink$>index.php
    </MTArchiveList>
    

    上記はブログのPHP化のときに使った、.htaccess用のコードになります。

    青い字の部分が現在のルートディレクトリからの絶対パスとなります。
    赤い字の部分がリダイレクト先のURLです。

    上記は、ディレクトリ名が変わったとか、ファイル名が変わったなど、同じサイト内でリダイレクトさせる例ですが、サーバー移転など、別なURLにリダイレクトしたい場合には以下のようなコードになります。

    # トップページをリダイレクトしたい場合
    Redirect permanent <$MTBlogURL relative_url="1"$>index.html ●●●/index.html
    # 各エントリをリダイレクトしたい場合
    <MTEntries lastn="0">
    Redirect permanent <$MTEntryPermalink relative_url="1"$> ●●●/<MTEntryDate format="%Y/%m/%d-%H%M%S"$>.html
    </MTEntries>
    # 各カテゴリをリダイレクトしたい場合
    <MTArchiveList archive_type="Category">
    Redirect permanent <$MTArchiveLink relative_url="1"$> ●●●/<$MTCategoryLabel dirify="1"$>/index.html
    </MTArchiveList>
    # 各月別ページをリダイレクトしたい場合
    <MTArchiveList archive_type="Monthly">
    Redirect permanent <$MTArchiveLink relative_url="1"$> ●●●/<MTArchiveDate format="%Y/%m/"$>index.html
    </MTArchiveList>
    # 各日別ページをリダイレクトしたい場合
    <MTArchiveList archive_type="Daily">
    Redirect permanent <$MTArchiveLink relative_url="1"$> ●●●/<MTArchiveDate format="%Y/%m/%d/"$>index.html
    </MTArchiveList>
    

    作成された htaccess.txt 内の青い字の部分にあたるところを、リダイレクト先のURLで一括置換すれば楽ですよね。
    また、個別エントリのファイル名が異なる場合は…大変ですが1つ1つ直すしかないです (; ̄∇ ̄A

  3. 一通り入力したら、保存してこのリダイレクト用のインデックス・テンプレートのみ再構築します。
    再構築するとindex.htmlと同じ場所にhtaccess.txtというファイルが作成されます。

リダイレクト先の内容を確認

htaccess.txtをダウンロードして、テキストエディタで開いてください。
そして、現在のURL、リダイレクト先のURLなど間違いがないか確認します。

リダイレクト先が間違ってると正しくリダイレクトされませんので注意してください。

以上で、リダイレクト用のhtaccess.txtというファイルが作られました。

このままではまだリダイレクトされません。
リダイレクト先の準備ができたら、サーバーにあるhtaccess.txtを.htaccessのようにファイル名を変更してください。 変更するとすぐにリダイレクトが有効になります。

実際にわたしのサイトでリダイレクトしてる例を以下に記します。

http://bizcaz.com/
|
+-- /archive/movabletype/template/ <-- ここ配下のページにアクセスされたらリダイレクトする
|     +-- index.php
|     +-- styles-site.css
|     |
|     +-- archives/
|     |    +-- 2006/
|     |    +-- 2007/
|
+-- .htaccess    <--- ここにおくことでリダイレクトされます

上記はディレクトリ以下に存在するエントリページ、月別ページにアクセスされたら、以下に示すリダイレクト先に変換するようにしています。

.htaccess
# 各エントリをリダイレクトしたい場合
Redirect permanent /archives/movabletype/template/archives/2007/04/01-021005.php http://bizcaz.com/archives/2007/04/01-021005.php
Redirect permanent /archives/movabletype/template/archives/2007/02/25-211414.php http://bizcaz.com/archives/2007/02/25-211414.php
Redirect permanent /archives/movabletype/template/archives/2007/02/25-002859.php http://bizcaz.com/archives/2007/02/25-002859.php
Redirect permanent /archives/movabletype/template/archives/2007/02/11-214915.php http://bizcaz.com/archives/2007/02/11-214915.php
Redirect permanent /archives/movabletype/template/archives/2007/02/05-195215.php http://bizcaz.com/archives/2007/02/05-195215.php
Redirect permanent /archives/movabletype/template/archives/2007/02/05-011951.php http://bizcaz.com/archives/2007/02/05-011951.php
Redirect permanent /archives/movabletype/template/archives/2007/01/21-194045.php http://bizcaz.com/archives/2007/01/21-194045.php
Redirect permanent /archives/movabletype/template/archives/2007/01/14-165458.php http://bizcaz.com/archives/2007/01/14-165458.php
Redirect permanent /archives/movabletype/template/archives/2007/01/08-171147.php http://bizcaz.com/archives/2007/01/08-171147.php
Redirect permanent /archives/movabletype/template/archives/2007/01/02-054526.php http://bizcaz.com/archives/2007/01/02-054526.php
Redirect permanent /archives/movabletype/template/archives/2006/12/31-130040.php http://bizcaz.com/archives/2006/12/31-130040.php
Redirect permanent /archives/movabletype/template/archives/2006/12/30-024643.php http://bizcaz.com/archives/2006/12/30-024643.php
Redirect permanent /archives/movabletype/template/archives/2006/12/29-231235.php http://bizcaz.com/archives/2006/12/29-231235.php
Redirect permanent /archives/movabletype/template/archives/2006/12/29-194141.php http://bizcaz.com/archives/2006/12/29-194141.php
Redirect permanent /archives/movabletype/template/archives/2006/12/23-144858.php http://bizcaz.com/archives/2006/12/23-144858.php
Redirect permanent /archives/movabletype/template/archives/2006/12/15-182221.php http://bizcaz.com/archives/2006/12/15-182221.php
Redirect permanent /archives/movabletype/template/archives/2006/12/10-193209.php http://bizcaz.com/archives/2006/12/10-193209.php
Redirect permanent /archives/movabletype/template/archives/2006/12/09-084354.php http://bizcaz.com/archives/2006/12/09-084354.php
Redirect permanent /archives/movabletype/template/archives/2006/12/07-022521.php http://bizcaz.com/archives/2006/12/07-022521.php
Redirect permanent /archives/movabletype/template/archives/2006/11/22-141550.php http://bizcaz.com/archives/2006/11/22-141550.php
Redirect permanent /archives/movabletype/template/archives/2006/10/07-142028.php http://bizcaz.com/archives/2006/10/07-142028.php
Redirect permanent /archives/movabletype/template/archives/2006/10/01-155923.php http://bizcaz.com/archives/2006/10/01-155923.php
Redirect permanent /archives/movabletype/template/archives/2006/09/30-022016.php http://bizcaz.com/archives/2006/09/30-022016.php
Redirect permanent /archives/movabletype/template/archives/2006/09/27-195040.php http://bizcaz.com/archives/2006/09/27-195040.php
Redirect permanent /archives/movabletype/template/archives/2006/09/27-185040.php http://bizcaz.com/archives/2006/09/27-185040.php
Redirect permanent /archives/movabletype/template/archives/2006/09/27-175040.php http://bizcaz.com/archives/2006/09/27-175040.php
Redirect permanent /archives/movabletype/template/archives/2006/09/27-174040.php http://bizcaz.com/archives/2006/09/27-174040.php
Redirect permanent /archives/movabletype/template/archives/2006/09/27-122445.php http://bizcaz.com/archives/2006/09/27-122445.php
Redirect permanent /archives/movabletype/template/archives/2006/09/16-183157.php http://bizcaz.com/archives/2006/09/16-183157.php
# 各月別ページをリダイレクトしたい場合
Redirect permanent /archives/movabletype/template/archives/2007/04/ http://bizcaz.com/archives/2007/04/index.php
Redirect permanent /archives/movabletype/template/archives/2007/02/ http://bizcaz.com/archives/2007/02/index.php
Redirect permanent /archives/movabletype/template/archives/2007/01/ http://bizcaz.com/archives/2007/01/index.php
Redirect permanent /archives/movabletype/template/archives/2006/12/ http://bizcaz.com/archives/2006/12/index.php
Redirect permanent /archives/movabletype/template/archives/2006/11/ http://bizcaz.com/archives/2006/11/index.php
Redirect permanent /archives/movabletype/template/archives/2006/10/ http://bizcaz.com/archives/2006/10/index.php
Redirect permanent /archives/movabletype/template/archives/2006/09/ http://bizcaz.com/archives/2006/09/index.php

青い字の部分がルートディレクトリ(わたしのサイトのURLでいうとhttp://bizcaz.com/)を基点にしたリダイレクト元ページの相対(絶対)パスです。
赤い字の部分がリダイレクト先のURLになります。

注意することは、上記青い字の部分は必ずウェブサイトのルートディレクトリからの相対(絶対)パスにする必要があります。

ディレクトリ名、ディレクトリ構成が変わったときなどに利用してくださいな ( ̄∇ ̄)/

SEE YOU♪

 Trackback Pings(0)

No trackbacks found.

 Comments(2)

#1: Posted by take [RES]

http://bizcaz.com/archives/shoco/mt/plugins/mt-relativepath.pl

上のURLにアクセスすると、404 Not Foundと表示されてDL出来ませんでした。
Movable Type備忘録さんのエントリーを参考にphp化したばかりなので、MTRelativePathのプラブインも是非インストールしたかったのです。
お時間がある時にでもまた公開して頂けたらと^^

#2: Posted by bzbell Author Profile Page [RES]

>>1 take さん

こんにちわ^^

> 上のURLにアクセスすると、404 Not Foundと表示されてDL出来ませんでした。

ガーーンあせあせ ショック!!
すいませんでした。

> お時間がある時にでもまた公開して頂けたらと^^

はい。今はお仕事中なので帰宅しましたら UP しておきます。

今後ともよろしくですのテレテレ

 Post a Comment

コメント用フィード