Search code examples
pythondjangoherokuhttp-redirectdns

Redirect all requests for www. to root domain


I need to redirect all requests coming from www.mysite.com to mysite.com

I have found the solution in rails, but how can I do that in Django/Python?

The only solution I could find, which was posted by a moderator on GoDaddy was the above. Seems like I cannot resolve this kind of problem through the DNS Manager of GoDaddy.


Solution

  • Solved with this:

    from django.http import HttpResponsePermanentRedirect
    
    class WWWRedirectMiddleware(object):
        def process_request(self, request):
            if request.META['HTTP_HOST'].startswith('www.'):
                return HttpResponsePermanentRedirect('http://example.com')