0

I have a middleware that redirects mobile users to a mobile site, but I want to direct them to the the full site if the url is /property/. The mobile redirect is working, but /property/ is not being excluded.

Here is the current middleware.

middleware.py

# Adapted from http://djangosnippets.org/snippets/2001/
import re
from  django.conf import settings
from django.http import HttpResponseRedirect


class MobileRedirectMiddleware(object):
    """
    Redirects mobile users to a different site.
    """
    def process_request(self, request):
        if self._is_mobile(request):
            return HttpResponseRedirect(settings.MOBILE_SITE_URL)

    def _is_mobile(self, request):
        is_mobile = False
        NON_MOBILE_REDIRECT_URLS = getattr(settings, 'NON_MOBILE_REDIRECT_URLS', [])
        if request.path in NON_MOBILE_REDIRECT_URLS:
            return False

        if request.META.has_key('HTTP_USER_AGENT'):
            user_agent = request.META['HTTP_USER_AGENT']

            # Test common mobile values.
            pattern = "(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|windows ce|pda|mobile|mini|palm|netfront)"
            prog = re.compile(pattern, re.IGNORECASE)
            match = prog.search(user_agent)

            if match:
                is_mobile = True
            else:
                # Nokia like test for WAP browsers.
                # http://www.developershome.com/wap/xhtmlmp/xhtml_mp_tutorial.asp?page=mimeTypesFileExtension

                if request.META.has_key('HTTP_ACCEPT'):
                    http_accept = request.META['HTTP_ACCEPT']

                    pattern = "application/vnd\.wap\.xhtml\+xml"
                    prog = re.compile(pattern, re.IGNORECASE)

                    match = prog.search(http_accept)

                    if match:
                        is_mobile = True

            if not is_mobile:
                # Now we test the user_agent from a big list.
                user_agents_test = ("w3c ", "acs-", "alav", "alca", "amoi", "audi",
                                    "avan", "benq", "bird", "blac", "blaz", "brew",
                                    "cell", "cldc", "cmd-", "dang", "doco", "eric",
                                    "hipt", "inno", "ipaq", "java", "jigs", "kddi",
                                    "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-",
                                    "maui", "maxo", "midp", "mits", "mmef", "mobi",
                                    "mot-", "moto", "mwbp", "nec-", "newt", "noki",
                                    "xda",  "palm", "pana", "pant", "phil", "play",
                                    "port", "prox", "qwap", "sage", "sams", "sany",
                                    "sch-", "sec-", "send", "seri", "sgh-", "shar",
                                    "sie-", "siem", "smal", "smar", "sony", "sph-",
                                    "symb", "t-mo", "teli", "tim-", "tosh", "tsm-",
                                    "upg1", "upsi", "vk-v", "voda", "wap-", "wapa",
                                    "wapi", "wapp", "wapr", "webc", "winw", "winw",
                                    "xda-",)

                test = user_agent[0:4].lower()
                if test in user_agents_test:
                    is_mobile = True

        return is_mobile

in settings.py I have this:

MOBILE_SITE_URL = 'http://mobile.somesite.com/'
NON_MOBILE_REDIRECT_URLS = ['/property/']
dokkaebi
  • 9,004
  • 3
  • 42
  • 60
tjoenz
  • 699
  • 5
  • 14
  • 39

1 Answers1

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
  • 9,004
  • 3
  • 42
  • 60
  • Thank you. I tried this but still getting redirected to the mobile site. – tjoenz Nov 13 '12 at 20:36
  • Could you step through that section of code and confirm that a) request.path is what you think it is, b) that `getattr(settings, 'NON_MOBILE_REDIRECT_URLS', [])` returns the list you think it does, and c) that the redirect is coming from this middleware? Could you add these values to your original question? – dokkaebi Nov 13 '12 at 20:40
  • Are you sure you included you costume middleware in you settings.py file ? – PepperoniPizza Nov 13 '12 at 20:51
  • @dokkaebi I have edited the above code and when I print request.path it prints my /property/ url path but still redirects – tjoenz Nov 13 '12 at 20:57
  • @PepperoniPizza yea, that is included and the reason the redirect is working – tjoenz Nov 13 '12 at 20:58
  • Hm, is it possible you were redirected, say, from `http://www.somesite.com/` to `http://mobile.somesite.com/` before you loaded `/property/`, and then you are loading `http://mobile.somesite.com/property/` from `http://mobile.somesite.com/` without a redirect at all? You say you checked the path, but did you also check the list of excludes and whether the redirect comes from this middleware? – dokkaebi Nov 13 '12 at 21:05
  • Hmmm, Well if I comment out this middleware, the site does not redirect and I get the full site on mobile – tjoenz Nov 13 '12 at 21:11
  • That suggests to me that you need no redirect to hit the mobile site, and therefore just not redirecting when the url is `/property/` doesn't help you. Instead, you might need to enforce a redirect to the full site in this case. I'll update the answer. – dokkaebi Nov 13 '12 at 22:01
  • Ok, going to give that a shot. I'll let you know what happens. Thank you @dokkaebi – tjoenz Nov 13 '12 at 22:30