Ticket #6342: patch_removewww.patch

File patch_removewww.patch, 2.3 KB (added by corporeal <corporeal@…>, 17 years ago)

Patch to add REMOVE_WWW optionA?

  • django/middleware/common.py

    old new  
    1313
    1414        - Forbids access to User-Agents in settings.DISALLOWED_USER_AGENTS
    1515
    16         - URL rewriting: Based on the APPEND_SLASH and PREPEND_WWW settings,
    17           this middleware appends missing slashes and/or prepends missing
    18           "www."s.
     16        - URL rewriting: Based on the APPEND_SLASH, PREPEND_WWW and REMOVE_WWW
     17          settings this middleware appends missing slashes and/or removes/prepends
     18          "www."s.
    1919
    2020            - If APPEND_SLASH is set and the initial URL doesn't end with a
    2121              slash, and it is not found in urlpatterns, a new URL is formed by
     
    4040                if user_agent_regex.search(request.META['HTTP_USER_AGENT']):
    4141                    return http.HttpResponseForbidden('<h1>Forbidden</h1>')
    4242
    43         # Check for a redirect based on settings.APPEND_SLASH
    44         # and settings.PREPEND_WWW
     43        # Check for a redirect based on settings.APPEND_SLASH,
     44        # settings.PREPEND_WWW, and settings.REMOVE_WWW
    4545        host = request.get_host()
    4646        old_url = [host, request.path]
    4747        new_url = old_url[:]
     
    5050                not old_url[0].startswith('www.')):
    5151            new_url[0] = 'www.' + old_url[0]
    5252
     53        # Remove the "www." subdomain from the url if REMOVE_WWW is set and
     54        # the url starts with "www."
     55        if (settings.REMOVE_WWW and old_url[0] and
     56                old_url[0].startswith('www.')):
     57            new_url[0] = new_url[0][4:]
     58       
     59
    5360        # Append a slash if APPEND_SLASH is set and the URL doesn't have a
    5461        # trailing slash and there is no pattern for the current path
    5562        if settings.APPEND_SLASH and (not old_url[1].endswith('/')):
  • django/conf/global_settings.py

    old new  
    179179# Whether to prepend the "www." subdomain to URLs that don't have it.
    180180PREPEND_WWW = False
    181181
     182# Whether to remove the "www." subdomain from URLs that have it.
     183REMOVE_WWW = False
     184
    182185# List of compiled regular expression objects representing User-Agent strings
    183186# that are not allowed to visit any page, systemwide. Use this for bad
    184187# robots/crawlers. Here are a few examples:
Back to Top