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:
<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.
- 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.