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
27 sub cgi_page_from_404 ($$$) {
32 # fail if missing from environment or whatever
33 return undef unless defined $path;
34 return undef unless defined $baseurl;
36 # with usedirs on, path is like /~fred/foo/bar/ or /~fred/foo/bar or
37 # /~fred/foo/bar/index.html
38 # with usedirs off, path is like /~fred/foo/bar.html
39 # baseurl is like 'http://people.example.com/~fred'
41 # convert baseurl to ~fred
42 unless ($baseurl =~ s{^https?://[^/]+/?}{}) {
46 # convert path to /~fred/foo/bar
48 $path =~ s/\/*(?:index\.$config{htmlext})?$//;
51 $path =~ s/\.$config{htmlext}$//;
55 unless ($path =~ s{^/*\Q$baseurl\E/*}{}) {
59 # special case for the index
70 if (exists $ENV{REDIRECT_STATUS} &&
71 $ENV{REDIRECT_STATUS} eq '404') {
72 my $page = cgi_page_from_404($ENV{REDIRECT_URL},
73 $config{url}, $config{usedirs});
74 IkiWiki::Plugin::goto::cgi_goto($cgi, $page);