Index: common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.611.2.20
diff -u -r1.611.2.20 common.inc
--- common.inc	9 Jul 2008 19:34:30 -0000	1.611.2.20
+++ common.inc	1 Sep 2008 07:33:57 -0000
@@ -1189,35 +1189,47 @@
     $clean_url = (bool)variable_get('clean_url', '0');
   }
 
-  $base = ($absolute ? $base_url . '/' : base_path());
+  // Preserve the original path before aliasing.
+  $original_path = $path;
+  // Prevent the global $base_url from being altered.
+  $current_base_url = $base_url;
 
   // The special path '<front>' links to the default front page.
-  if (!empty($path) && $path != '<front>') {
+  if ($path == '<front>') {
+    $path = '';
+  } else if (!empty($path) && $path != '<front>') {
     $path = drupal_get_path_alias($path);
-    $path = drupal_urlencode($path);
-    if (!$clean_url) {
-      if (isset($query)) {
-        return $base . $script .'?q='. $path .'&'. $query . $fragment;
-      }
-      else {
-        return $base . $script .'?q='. $path . $fragment;
-      }
+  }
+
+  if (function_exists('custom_url_rewrite_outbound')) {
+    // Modules may alter outbound links by reference.
+    custom_url_rewrite_outbound($path, $query, $fragment, $absolute, $current_base_url, $original_path);
+  }
+
+  $base = ($absolute ? $current_base_url . '/' : base_path());
+  $path = drupal_urlencode($path);
+
+  if (!$clean_url) {
+    $variables = array();
+    if (!empty($path)) {
+      $variables[] = 'q='. $path;
+    }
+    if (isset($query)) {
+      $variables[] = $query;
+    }
+    if ($query = join('&', $variables)) {
+      return $base . $script .'?'. $query . $fragment;
     }
     else {
-      if (isset($query)) {
-        return $base . $path .'?'. $query . $fragment;
-      }
-      else {
-        return $base . $path . $fragment;
-      }
+      return $base . $fragment;
     }
   }
   else {
     if (isset($query)) {
-      return $base . $script .'?'. $query . $fragment;
+      return $base . $path .'?'. $query . $fragment;
     }
     else {
-      return $base . $fragment;
+      return $base . $path . $fragment;
     }
   }
 }

