2 # Copyright © 2009 Simon McVittie <http://smcv.pseudorandom.co.uk/>
3 # Licensed under the GNU GPL, version 2, or any later version published by the
4 # Free Software Foundation
5 package IkiWiki::Plugin::404;
12 hook(type => "cgi", id => '404', call => \&cgi);
13 IkiWiki::loadplugin("goto");
19 # not really a matter of safety, but enabling/disabling
20 # through a web interface is useless - it needs web
21 # server admin action too
28 sub cgi_page_from_404 ($$$) {
33 # fail if missing from environment or whatever
34 return undef unless defined $path;
35 return undef unless defined $baseurl;
37 # with usedirs on, path is like /~fred/foo/bar/ or /~fred/foo/bar or
38 # /~fred/foo/bar/index.html
39 # with usedirs off, path is like /~fred/foo/bar.html
40 # baseurl is like 'http://people.example.com/~fred'
42 # convert baseurl to ~fred
43 unless ($baseurl =~ s{^https?://[^/]+/?}{}) {
47 # convert path to /~fred/foo/bar
49 $path =~ s/\/*(?:index\.$config{htmlext})?$//;
52 $path =~ s/\.$config{htmlext}$//;
56 unless ($path =~ s{^/*\Q$baseurl\E/*}{}) {
60 # special case for the index
71 if (exists $ENV{REDIRECT_STATUS} &&
72 $ENV{REDIRECT_STATUS} eq '404') {
73 my $page = cgi_page_from_404(
74 Encode::decode_utf8($ENV{REDIRECT_URL}),
75 $config{url}, $config{usedirs});
76 IkiWiki::Plugin::goto::cgi_goto($cgi, $page);