Index: includes/bootstrap.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v
retrieving revision 1.60
diff -u -F^f -r1.60 bootstrap.inc
--- includes/bootstrap.inc	5 Aug 2005 00:49:02 -0000	1.60
+++ includes/bootstrap.inc	17 Aug 2005 11:45:23 -0000
@@ -680,6 +680,7 @@ function arg($index) {
 
   if (empty($arguments) || $q != $_GET['q']) {
     $arguments = explode('/', $_GET['q']);
+    $q = $_GET['q'];
   }
 
   if (isset($arguments[$index])) {
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.467
diff -u -F^f -r1.467 common.inc
--- includes/common.inc	16 Aug 2005 18:06:18 -0000	1.467
+++ includes/common.inc	17 Aug 2005 11:45:24 -0000
@@ -1576,7 +1579,7 @@ function form_clean_id($id = NULL) {
 function url($path = NULL, $query = NULL, $fragment = NULL, $absolute = FALSE) {
   global $base_url;
 
-  static $script;
+  static $script, $rewrites;
 
   if (empty($script)) {
     // On some web servers, such as IIS, we can't omit "index.php".  So, we
@@ -1587,6 +1590,17 @@ function url($path = NULL, $query = NULL
 
   $path = drupal_get_path_alias($path);
 
+  if (!isset($rewrites)) {
+    // we use array_reverse because restore will be done by hook_init
+    // implementations which are called in normal order
+    $rewrites = array_reverse(module_implements('url_rewrite'));
+  }
+  foreach($rewrites as $module) {
+    $function = $module .'_url_rewrite';
+    // we can't use module_invoke because all these four can be references
+    $function($path, $query, $fragment, $absolute);
+  }
+
   if (isset($fragment)) {
     $fragment = '#'. $fragment;
   }
