0

I'm trying to permanently redirect a legacy root URL to the new location in Django (hosted on Red Hat Openshift).

I've tried this solution but can't get it to work (even if the simplest case of http and without a further path). I'm not experienced with wsgi as you can probably guess and all help is very appreciated.

Here's my attempt to edit the last part of wsgi.py (redirecting from www.olddomain.com to www.newdomain.com). When I try to deploy it, trying to reach www.olddomain.com results in a error ("Can't reach this page"):

...
from django.core.wsgi import get_wsgi_application

_application = get_wsgi_application()

def application(environ, start_response):
  if environ['HTTP_HOST'][:17] == 'www.olddomain.com':
    start_response('301 Redirect', [('Location', 'http://www.newdomain.com/'),])
    return []

  return _application(environ, start_response)

Thank you for your help

Community
  • 1
  • 1
ptav
  • 171
  • 1
  • 11
  • Also, the `[:21]` may not be doing what you think it is. For example, `'This is an example of a string slice'[:21]` outputs `This is an example of` (the first 21 characters). Depending on your domain's string length, that may or may not work. – FlipperPA Jun 13 '16 at 18:14
  • Thanks, I've fixed the slice to fit the example domain but otherwise the idea is to strip out additional path of GET information (not sure if HTTP_HOST would carry that info though) – ptav Jun 14 '16 at 12:30

1 Answers1

0

Check your indentation level. Also, ensure that the value of the HTTP_HOST environment variable is the old domain.

from django.core.handlers.wsgi import WSGIHandler


_application = WSGIHandler()


def application(environ, start_response):
    if environ['HTTP_HOST'][:21] != 'www.example.com':
        start_response('301 Redirect', [
            ('Location', 'http://www.example.com/'),
        ])
        return []
    return _application(environ, start_response)
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
  • Indentation fixed (was a problem with the post, not original code). Thanks for pointing it out – ptav Jun 14 '16 at 12:31