--- pearwiki_filter.module.1.11	2008-02-26 17:15:38.000000000 +0100
+++ pearwiki_filter.module	2008-02-26 17:20:09.000000000 +0100
@@ -553,14 +553,19 @@ function pearwiki_filter_page_path($page
     $path = 'wiki/'. urlencode($page);
   }
   else {
-    // Try to find the node and link to it directly.
-    $node = db_fetch_object(db_query("SELECT nid FROM {node} WHERE LOWER(title)=LOWER('%s')", $page));
+    // Try to find the node and link to it directly. 
+    // At this time spaces in $page are replaced by $space_replacement. For this query replace spaces in the node title on-the-fly too.
+    // Note: If PEAR Wiki Filter Format is set to 'Mediawiki' $space_replacement is always an underscore.
+    $space_replacement = (pearwiki_filter_syntax($pearwiki_current_format) == 'Mediawiki') ? '_' : $space_replacement;
+    $node = db_fetch_object(db_query("SELECT nid FROM {node} WHERE LOWER(REPLACE(title, ' ', '%s'))=LOWER('%s')", $space_replacement, $page));
     if ($node) {
-      $path = 'node/'. $node->nid;
+      // Get the URL alias for this node if any
+      $path = drupal_get_path_alias('node/'.$node->nid);
     }
     else {
       // The page was not found.
-      $path = pearwiki_filter_wikilink_base($pearwiki_current_format) . urlencode($page);
+      // urlencode() changes slashes to %2F which results in invalide paths. Rechange %2F to slashes using str_replace()
+      $path = pearwiki_filter_wikilink_base($pearwiki_current_format) . str_replace("%2F", "/", urlencode($page));
     }
   }
   return (variable_get('clean_url', 0) ? '' : '?q=') . $path;
