The Django request URL becomes strange

advertisements

I have a django project running on my localhost and it is working very well, however when I uploaded it to real server, some problem started happening with the url. it happens every time HttpResponseRedirect or any redirect gets called

a page on my local host

http://127.0.0.1:8000/signin

while on the server it becomes

http://xyz.com,%20xyz.com/signin

in firebug i see

GET signin 301 MOVED PERMANENTLY
GET signin http://xyz.com,%20xyz.com/signin

I belive this happens because the urls.py has ^signin/$^ and APPEND_SLASH = True in settings.py because when I visit /signin/ it works!


404 page on my local host

Request URL:    http://127.0.0.1:8000/test

on the server

Request URL:    http://xyz.com,%20xyz.com/test

for some reason it is adding [comma][space] to url and redirects it.


home page is working without issues


The issue is tracked in the following ticket:
https://code.djangoproject.com/ticket/11877

It has to do with how Django handles proxy redirection. The following middleware will help you out.

class MultipleProxyMiddleware(object):
    FORWARDED_FOR_FIELDS = [
        'HTTP_X_FORWARDED_FOR',
        'HTTP_X_FORWARDED_HOST',
        'HTTP_X_FORWARDED_SERVER',
    ]

    def process_request(self, request):
        """
        Rewrites the proxy headers so that only the most
        recent proxy is used.
        """
        for field in self.FORWARDED_FOR_FIELDS:
            if field in request.META:
                if ',' in request.META[field]:
                    parts = request.META[field].split(',')
                    request.META[field] = parts[-1].strip()

If, for example, your Django site is sitting behind a proxy which includes proxy information in the X-Forwarded-For header, and then your web server also does proxying, the header will contain a list (comma separated) of the proxied addresses. By using this middleware, it will strip all but one of the proxied addresses in the headers.