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::apache404;
12 hook(type => "cgi", id => 'apache404', call => \&cgi);
18 # not really a matter of safety, but enabling/disabling
19 # through a web interface is useless - it needs web
20 # server admin action too
26 sub cgi_page_from_404 ($$$) {
31 # fail if missing from environment or whatever
32 return undef unless defined $path;
33 return undef unless defined $baseurl;
35 # with usedirs on, path is like /~fred/foo/bar/ or /~fred/foo/bar or
36 # /~fred/foo/bar/index.html
37 # with usedirs off, path is like /~fred/foo/bar.html
38 # baseurl is like 'http://people.example.com/~fred'
40 # convert baseurl to ~fred
41 unless ($baseurl =~ s{^https?://[^/]+/?}{}) {
45 # convert path to /~fred/foo/bar
47 $path =~ s/\/*(?:index\.$config{htmlext})?$//;
50 $path =~ s/\.$config{htmlext}$//;
54 unless ($path =~ s{^/*\Q$baseurl\E/*}{}) {
58 # special case for the index
69 if ($ENV{REDIRECT_STATUS} eq '404') {
70 my $page = cgi_page_from_404($ENV{REDIRECT_URL},
71 $config{url}, $config{usedirs});
72 IkiWiki::cgi_goto($cgi, $page);