#28359: SecurityMiddleware's SECURE_SSL_HOST only affects unsecure requests
-------------------------------------+-------------------------------------
     Reporter:  Matthias Kestenholz  |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  HTTP handling        |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  1                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Matthias Kestenholz:

Old description:

> For search engine optimization purposes it is preferable to have a single
> domain serving all requests. We have a custom middleware in most projects
> which handles this but dropped the middleware for sites using SSL:
>

> {{{
> from django.conf import settings
> from django.http import HttpResponsePermanentRedirect
>

> def force_domain(get_response):
>     if settings.DEBUG or not settings.FORCE_DOMAIN:
>         return get_response
>
>     def middleware(request):
>         if request.method != 'GET':
>             return get_response(request)
>
>         if request.get_host() != settings.FORCE_DOMAIN:
>             target = 'http%s://%s%s' % (
>                 request.is_secure() and 's' or '',
>                 settings.FORCE_DOMAIN,
>                 request.get_full_path())
>             return HttpResponsePermanentRedirect(target)
>
>         return get_response(request)
>
>     return middleware
> }}}
>
> It seems to me that setting `SECURE_SSL_REDIRECT` and `SECURE_SSL_HOST`
> should also handle the case where 1. a request already uses a secure
> connection but 2. the host does not equal `SECURE_SSL_HOST`.
>
> (Categorized as new feature because that's more fun.)

New description:

 For search engine optimization purposes it is preferable to have a single
 domain serving all requests. We have a custom middleware in most projects
 which handles this but dropped the middleware for sites using SSL:


 {{{
 from django.conf import settings
 from django.http import HttpResponsePermanentRedirect


 def force_domain(get_response):
     if settings.DEBUG or not settings.FORCE_DOMAIN:
         return get_response

     def middleware(request):
         if request.method != 'GET':
             return get_response(request)

         if request.get_host() != settings.FORCE_DOMAIN:
             target = 'http%s://%s%s' % (
                 request.is_secure() and 's' or '',
                 settings.FORCE_DOMAIN,
                 request.get_full_path())
             return HttpResponsePermanentRedirect(target)

         return get_response(request)

     return middleware
 }}}

 We noticed today that setting `SECURE_SSL_HOST = 'example.com'` does not
 redirect requests to `https://www.example.com`

 It seems to me that setting `SECURE_SSL_REDIRECT` and `SECURE_SSL_HOST`
 should also handle the case where 1. a request already uses a secure
 connection but 2. the host does not equal `SECURE_SSL_HOST`.

 (Categorized as new feature because that's more fun.)

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28359#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.3ac5c672abe8991a2e68d3d3882ade23%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to