nofound plug-in †
徒書 製 notfound プラグイン の機能に、さらに存在しないインデックスページに対しても「 404 not found 」を返すためのプラグインです。
個人的な趣味で一部書き換えていますが、「 404 not found 」を返す部分に関しては基本オリジナルの notfound プラグイン と同じです。
プラグイン本体の編集 †
デフォルトのままでも動作しますが、必要に応じて以下の変数の値を変更してください。
- @except_flavours
- アクセスされた URL に該当するファイルが存在しない場合でも「 404 not found 」を出力しない フレーバー を指定します。コンマ (,) 区切り(リスト形式)で列挙してください。初期設定では wikieditishフレーバー のみが設定されています。
- @noindex_flavours
- index ページが存在するはずのないフレーバーを指定して、その index ページへのアクセスに対して、「 404 not found 」を返すようにします。初期設定では writebackフレーバー のみが指定されています。
アップロード †
プラグイン本体をプラグインディレクトリーにアップロードすれば機能します。
ちなみにオリジナルの notfound プラグイン からの機能で、データ保存ディレクトリに page.notfound というテンプレートを作ると、「 404 not found 」を返す際のページとして表示することができます。このページでは、blosxom 本体とインストール済みプラグインの変数以外に、以下の変数が使えます。
- $nofound::url
- アクセスされたときの url
- $nofound::signature
- ホスト名などを含むサーバ情報
Apr 17, 2007 版全ソースリスト
# Blosxom Plugin: nofound
# originate from "notfound plugin" by http://www.akatsukinishisu.net/itazuragaki/
# Author: kay <info@ellinikonblue.com>
# Version: 2007-04-08
# This script is encoded in UTF-8.
package nofound;
# --- Configurable variables -----------
my @except_flavours = ( 'wikieditish' ); # not found 出力を避けるフレーバー
my @noindex_flavours = ( 'writeback' ); # index へのアクセスを not found にするフレーバー
# --- Plug-in package variables --------
use vars qw($url $signature);
my $stories = 0;
# --------------------------------------
use strict;
use CGI qw/:standard/;
sub start {
my $flavourlist = join( "|", @except_flavours );
return 0 if ($blosxom::static_or_dynamic eq 'static');
return 0 if ($blosxom::flavour =~ m/$flavourlist/i );
return 1;
}
sub filter {
my( $pkg, $files_ref, $others_ref ) = @_;
my $path_dir = "$blosxom::datadir/$blosxom::path_info";
return 1 unless ($blosxom::path_info);
return 1 if ($blosxom::path_info =~ m{[^/]+\.[^/]+$});
# $path_infoのディレクトリ部分に一致しないファイルを除去
foreach my $f (keys %$files_ref) {
$f =~ m{^$path_dir/} or delete($files_ref->{$f});
}
return 1;
}
sub story {
$stories += 1; # 記事数を数える
return 1;
}
sub last {
my $path_info = CGI::path_info();
my ( $path, $fn ) = $path_info =~ m!^(?:(.*)/)?(.*)\.$blosxom::flavour!;
my $flavourlist = join( "|", @noindex_flavours );
# 記事があり、許可しないフレーバーのインデックスページへのアクセスでないのなら復帰
if ( $stories ) {
if ( $blosxom::flavour !~ m/$flavourlist/i ) {
return 1;
} else {
if ( $fn !~ m/index/i ) {
return 1;
}
}
}
# 記事が無かったら404 Not Foundを返す
$blosxom::header->{-status} = '404 Not Found';
# テンプレート用変数の設定
$url = url(-absolute => 1, -path => 1);
$signature = server_software();
$signature =~ s{(^\w+/[\d.]+).*$}{$1};
$signature .= ' Server at ' . server_name() . ' Port ' . server_port();
# テンプレート読み込み
while (<DATA>) {
last if (/^__END__$/);
my ($flavour, $comp, $text) = split(' ', $_, 3);
$text =~ s/\\n/\n/g;
$blosxom::template{$flavour}{$comp} = $text;
}
# Not foundページの Content-Type 処理
my $content_type = &$blosxom::template($blosxom::path_info, 'content_type', 'notfound');
$content_type =~ s/\n.*//s;
$blosxom::header->{-type} = $content_type;
# Not foundページの処理
my $page = &$blosxom::template($blosxom::path_info, 'page', 'notfound');
$blosxom::output = &$blosxom::interpolate($page);
return 1;
}
1;
__DATA__
notfound content_type text/html
notfound page <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n<html lang="en">\n<head>\n<title>404 Not Found</title>\n</head>\n<body>\n<h1>Not Found</h1>\n<p>The requested URL $notfound::url was not found on this server.</p>\n<hr>\n<address>$notfound::signature</address>\n</body>\n</html>\n
__END__