Index: http/__init__.py
===================================================================
--- http/__init__.py	(revision 4285)
+++ http/__init__.py	(working copy)
@@ -302,3 +302,18 @@
     if not host:
         host = request.META.get('HTTP_HOST', '')
     return host
+
+def build_url(request, path, host=None):
+    "Builds an absolute URL with the given path."
+    if not host:
+        host = get_host(request)
+        
+    if host:
+        newurl = "%s://%s%s" % (request.is_secure() and 'https' or 'http', host, path)
+    else:
+        newurl = path
+        
+    if request.GET:
+        newurl += '?' + request.GET.urlencode()
+        
+    return newurl
Index: middleware/common.py
===================================================================
--- middleware/common.py	(revision 4285)
+++ middleware/common.py	(working copy)
@@ -44,12 +44,7 @@
                 raise RuntimeError, "You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to %s%s (note the trailing slash), or set APPEND_SLASH=False in your Django settings." % (new_url[0], new_url[1])
         if new_url != old_url:
             # Redirect
-            if new_url[0]:
-                newurl = "%s://%s%s" % (request.is_secure() and 'https' or 'http', new_url[0], new_url[1])
-            else:
-                newurl = new_url[1]
-            if request.GET:
-                newurl += '?' + request.GET.urlencode()
+            newurl = http.build_url(request, host=new_url[0], path=new_url[1])
             return http.HttpResponsePermanentRedirect(newurl)
 
         return None
