--- django/middleware/common.py.old 2008-01-08 03:28:00.000000000 -0800
+++ django/middleware/common.py 2008-01-08 03:19:03.000000000 -0800
@@ -13,9 +13,9 @@
- Forbids access to User-Agents in settings.DISALLOWED_USER_AGENTS
- - URL rewriting: Based on the APPEND_SLASH and PREPEND_WWW settings,
- this middleware appends missing slashes and/or prepends missing
- "www."s.
+ - URL rewriting: Based on the APPEND_SLASH, PREPEND_WWW and REMOVE_WWW
+ settings this middleware appends missing slashes and/or removes/prepends
+ "www."s.
- If APPEND_SLASH is set and the initial URL doesn't end with a
slash, and it is not found in urlpatterns, a new URL is formed by
@@ -40,8 +40,8 @@
if user_agent_regex.search(request.META['HTTP_USER_AGENT']):
return http.HttpResponseForbidden('
Forbidden
')
- # Check for a redirect based on settings.APPEND_SLASH
- # and settings.PREPEND_WWW
+ # Check for a redirect based on settings.APPEND_SLASH,
+ # settings.PREPEND_WWW, and settings.REMOVE_WWW
host = request.get_host()
old_url = [host, request.path]
new_url = old_url[:]
@@ -50,6 +50,13 @@
not old_url[0].startswith('www.')):
new_url[0] = 'www.' + old_url[0]
+ # Remove the "www." subdomain from the url if REMOVE_WWW is set and
+ # the url starts with "www."
+ if (settings.REMOVE_WWW and old_url[0] and
+ old_url[0].startswith('www.')):
+ new_url[0] = new_url[0][4:]
+
+
# Append a slash if APPEND_SLASH is set and the URL doesn't have a
# trailing slash and there is no pattern for the current path
if settings.APPEND_SLASH and (not old_url[1].endswith('/')):
--- django/conf/global_settings.py.old 2008-01-08 03:28:14.000000000 -0800
+++ django/conf/global_settings.py 2008-01-08 03:19:38.000000000 -0800
@@ -179,6 +179,9 @@
# Whether to prepend the "www." subdomain to URLs that don't have it.
PREPEND_WWW = False
+# Whether to remove the "www." subdomain from URLs that have it.
+REMOVE_WWW = False
+
# List of compiled regular expression objects representing User-Agent strings
# that are not allowed to visit any page, systemwide. Use this for bad
# robots/crawlers. Here are a few examples: