+Index: IkiWiki.pm
+===================================================================
+--- IkiWiki.pm (revision 2806)
++++ IkiWiki.pm (working copy)
+@@ -14,7 +14,7 @@
+ use Exporter q{import};
+ our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
+ bestlink htmllink readfile writefile pagetype srcfile pagename
+- displaytime will_render gettext
++ displaytime will_render gettext urlto targetpage
+ %config %links %renderedfiles %pagesources);
+ our $VERSION = 1.02; # plugin interface version, next is ikiwiki version
+ our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
+@@ -73,6 +73,7 @@
+ sslcookie => 0,
+ httpauth => 0,
+ userdir => "",
++ usedirs => 0
+ } #}}}
+
+ sub checkconfig () { #{{{
+@@ -224,10 +225,21 @@
+ return $page;
+ } #}}}
+
++sub targetpage ($$) { #{{{
++ my $page=shift;
++ my $ext=shift;
++
++ if (! $config{usedirs} || $page =~ /^index$/ ) {
++ return $page.".".$ext;
++ } else {
++ return $page."/index.".$ext;
++ }
++} #}}}
++
+ sub htmlpage ($) { #{{{
+ my $page=shift;
+-
+- return $page.".html";
++
++ return targetpage($page, "html");
+ } #}}}
+
+ sub srcfile ($) { #{{{
+@@ -393,6 +405,7 @@
+
+ return "$config{url}/" if ! defined $page;
+
++ $page=htmlpage($page);
+ $page=~s/[^\/]+$//;
+ $page=~s/[^\/]+\//..\//g;
+ return $page;
+@@ -422,6 +435,32 @@
+ $config{timeformat}, localtime($time)));
+ } #}}}
+
++sub beautify_url ($) { #{{{
++ my $url=shift;
++
++ $url =~ s!/index.html$!/!;
++ $url =~ s!^$!./!; # Browsers don't like empty links...
++
++ return $url;
++} #}}}
++
++sub urlto ($$) { #{{{
++ my $to=shift;
++ my $from=shift;
++
++ if (! length $to) {
++ return beautify_url(baseurl($from));
++ }
++
++ if (! grep { $_ eq $to } map { @{$_} } values %renderedfiles) {
++ $to=htmlpage($to);
++ }
++
++ my $link = abs2rel($to, dirname(htmlpage($from)));
++
++ return beautify_url($link);
++} #}}}
++
+ sub htmllink ($$$;@) { #{{{
+ my $lpage=shift; # the page doing the linking
+ my $page=shift; # the page that will contain the link (different for inline)
+@@ -457,7 +496,8 @@
+ "\">?</a>$linktext</span>"
+ }
+
+- $bestlink=abs2rel($bestlink, dirname($page));
++ $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
++ $bestlink=beautify_url($bestlink);
+
+ if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
+ return "<img src=\"$bestlink\" alt=\"$linktext\" />";