[Answer]-Why does my redirect middleware fail?

1πŸ‘

βœ…

It may not be enough just to avoid redirecting to mobile. at the given url. If the user is already coming from mobile.somesite.com/..../, you will need to actively redirect them to www. to get away from the mobile site.

This is untested, but should be pretty close:

class MobileRedirectMiddleware(object):
    """
    Redirects mobile users to a different site.
    """
    def process_request(self, request):
        was_mobile = settings.MOBILE_SITE_URL in request.META.HTTP_REFERER
        NON_MOBILE_REDIRECT_URLS = getattr(settings, 'NON_MOBILE_REDIRECT_URLS', [])

        if request.path in NON_MOBILE_REDIRECT_URLS and was_mobile:
            # redirect them to 'www.somesite.com/.../'
            return HttpResponseRedirect(settings.MAIN_SITE_URL + request.path.lstrip('/'))

        if self._is_mobile(request):
            return HttpResponseRedirect(settings.MOBILE_SITE_URL)

    def _is_mobile(self, request):
        is_mobile = False

        # no longer need to check urls in here

        if request.META.has_key('HTTP_USER_AGENT'):
             ...
πŸ‘€dokkaebi

Leave a comment