Search code examples
djangohttp-redirectprotocols

Django redirect not working when specifying HTTP or HTTPS


I am not sure why this is happening, but when I specify HTTP or HTTPS as my full URL in a redirect, the part after my domain name is appended to my current domain.

For example: if I redirect to https://www.external_site.com/error/page/hi_there.html it will go to https://www.currentdomain.com/error/hi_there/html

return redirect('https://www.external_site.com/error/page/hi_there.html')

But, when I remove the https: part (but leave the //), the redirect works as expected:

return redirect('//www.external_site.com/error/page/hi_there.html')

I am using Django v 1.11.23 but also tested it on Django 2.

Django runs on Apache on mod_wsgi, and goes through an IIS reverse proxy (the reverse proxy is just a reverse proxy in this instance, no special rules or anything besides to rewrite the external domain to the internal domain.)


Solution

  • I found the issue.

    It is related to "IIS reverse rewrite host in response headers" ARR setting. I disabled it by going to "IIS Manager -> Machine or Site (I used Machine so it applies to all sites) -> Application Request Routing Cache -> Server Proxy Settings and uncheck the "Reverse rewrite host in response headers" checkbox“

    Now it is working as it should.