Index: django/middleware/common.py
===================================================================
--- django/middleware/common.py	(revisión: 9110)
+++ django/middleware/common.py	(copia de trabajo)
@@ -56,32 +56,32 @@
             try:
                 urlresolvers.resolve(request.path_info)
             except urlresolvers.Resolver404:
-                new_url[1] = new_url[1] + '/'
-                if settings.DEBUG and request.method == 'POST':
-                    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])
+                # Check if slash ending path exists
+                try:
+                    urlresolvers.resolve("%s/" % request.path_info)
+                except urlresolvers.Resolver404:
+                    pass
+                else:
+                    new_url[1] = new_url[1] + '/'
+                    if settings.DEBUG and request.method == 'POST':
+                        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 the target url exists
-            try:
-                urlresolvers.resolve("%s/" % request.path_info)
-            except urlresolvers.Resolver404:
-                pass
+            if new_url[0]:
+                newurl = "%s://%s%s" % (
+                    request.is_secure() and 'https' or 'http',
+                    new_url[0], urlquote(new_url[1]))
             else:
-                if new_url[0]:
-                    newurl = "%s://%s%s" % (
-                        request.is_secure() and 'https' or 'http',
-                        new_url[0], urlquote(new_url[1]))
-                else:
-                    newurl = urlquote(new_url[1])
-                if request.GET:
-                    newurl += '?' + request.META['QUERY_STRING']
-                return http.HttpResponsePermanentRedirect(newurl)
+                newurl = urlquote(new_url[1])
+            if request.GET:
+                newurl += '?' + request.META['QUERY_STRING']
+            return http.HttpResponsePermanentRedirect(newurl)
 
         return None
 
Index: tests/regressiontests/middleware/tests.py
===================================================================
--- tests/regressiontests/middleware/tests.py	(revisión: 9110)
+++ tests/regressiontests/middleware/tests.py	(copia de trabajo)
@@ -89,3 +89,31 @@
         self.assertEquals(
             r['Location'],
             'http://testserver/middleware/needsquoting%23/')
+
+    def test_prepend_www(self):
+        settings.PREPEND_WWW = True
+        settings.APPEND_SLASH = False
+        request = self._get_request('path/')
+        r = CommonMiddleware().process_request(request)
+        self.assertEquals(r.status_code, 301)
+        self.assertEquals(
+            r['Location'],
+            'http://www.testserver/middleware/path/')
+
+    def test_prepend_www_append_slash_have_slash(self):
+        settings.PREPEND_WWW = True
+        settings.APPEND_SLASH = True
+        request = self._get_request('slash/')
+        r = CommonMiddleware().process_request(request)
+        self.assertEquals(r.status_code, 301)
+        self.assertEquals(r['Location'],
+                          'http://www.testserver/middleware/slash/')
+
+    def test_prepend_www_append_slash_slashless(self):
+        settings.PREPEND_WWW = True
+        settings.APPEND_SLASH = True
+        request = self._get_request('slash')
+        r = CommonMiddleware().process_request(request)
+        self.assertEquals(r.status_code, 301)
+        self.assertEquals(r['Location'],
+                          'http://www.testserver/middleware/slash/')
