Created
May 30, 2016 12:35
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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