1

I apologize in advance for asking this question since it has been asked many times but i could not find a Django specific solution which can fix this issue.

Description:

  • Django: 2.1
  • Python: 3.7
  • App Host: Google app engine
  • Domain Provider: Google domains

I have a Django App hosted on google app engine with [id].appspot.com url. I have also registered a custom domain e.g. www.example.com on google domains. I want to redirect anyone trying to access [id].appspot.com to www.example.com parmanently (301). My custom domain is working fine.

So far based on google and stackoverflow searches I have found the following:

  1. Add following canonical tag to each page
<link rel="canonical" href="https://www.example.com/" />

This should notify search engines which domain you prefer when same content is available from multiple domains. I have added this to my site header, but this seems like a workaround.

  1. Use check and redirect function: This post has some good approaches for webapp2 apps. However it is 10 years old post so not sure if any of these solution directly work or may need code changes.

In the answer "Alex Martelli" suggested to check os.environ['HTTP_HOST'].endswith('.appspot.com') and redirect. I am not sure how to accomplish it in Django app hosted on app engine.

In the same post, there is another way to achieve it by overriding webapp2.RequestHandler.initialize. but again i don't think it is applicable to Django.

I am looking for Django specific answer whether a redirect setting in app.yaml or dispatch.yaml similar to this blog post or a django function override to redirect from [id].appspot.com to www.example.com.

ravi
  • 1,078
  • 2
  • 17
  • 31

2 Answers2

0

There are no URl redirecttion capabilities at the app.yaml or dispatch.yaml levels.

The references you make are fairly old, applicable to the GAE 1st generation standard environment, not yours (you have python 3.7, so that's either the flexible environment or the 2nd generation standard environment)

You need to take care of the redirection inside you app itself, so you only need to check just for Django-specific approaches, pretty much forgetting about GAE from this perspective.

I'm not really a Django user, but I think these might be useful:

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
0

Finally I got it working with following code, however it requires extensive testing.

    from django.http import HttpResponsePermanentRedirect
    from django.utils.deprecation import MiddlewareMixin

    SITE_DOMAIN = "www.example.com"

    class CanonicalDomainMiddleware(MiddlewareMixin):    
        response_redirect_class = HttpResponsePermanentRedirect

        def process_request(self, request):

            host = request.get_host()

            if host == 'id.appspot.com':
                redirect_url = ('%s://%s%s' % (request.scheme, 
SITE_DOMAIN,request.get_full_path()))
                return self.response_redirect_class(redirect_url)
            else:
                return

Please let me know if anyone have any suggestions.

ravi
  • 1,078
  • 2
  • 17
  • 31