Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinsaka/5fa7830108d9ebbd4f80774c237f6fc4 to your computer and use it in GitHub Desktop.
Save shinsaka/5fa7830108d9ebbd4f80774c237f6fc4 to your computer and use it in GitHub Desktop.
redirects to correct hostname if requested hostname and settings.CORRECT_HOST does not match
from django.http import HttpResponsePermanentRedirect
from django.conf import settings
class RedirectCorrectHostname(object):
"""
redirects to correct hostname if requested hostname and settings.CORRECT_HOST does not match
(at heroku domain redirects to custom domain)
"""
def process_request(self, request):
if not getattr(settings, 'CORRECT_HOST', None):
return None
if request.get_host() == settings.CORRECT_HOST:
return None
return HttpResponsePermanentRedirect(
'{scheme}://{host}{path}'.format(scheme=request.scheme,
host=settings.CORRECT_HOST,
path=request.get_full_path()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment