Block requests from *.appspot.com and force custom domain in Google App Engine

google-app-engine, python, redirect

Solution

You can check if `os.environ['HTTP_HOST'].endswith('.appspot.com')` -- if so, then you're serving from `something.appspot.com` and can send a redirect, or otherwise alter your behavior as desired.

You could deploy this check-and-redirect-if-needed (or other behavior alteration of your choice) in any of various ways (decorators, WSGI middleware, inheritance from an intermediate base class of yours that subclasses `webapp.RequestHandler` [[or whatever other base handler class you're currently using]] and method names different than get and post in your application-level handler classes, and others yet) but I think that the key idea here is that `os.environ` is set by the app engine framework according to CGI standards and so you can rely on those standards (similarly WSGI builds its own environment based on the values it picks up from os.environ).

Problem

How can I prevent a user from accessing my app at example.appspot.com and force them to access it at example.com? I already have example.com working, but I don't want users to be able to access the appspot domain. I'm using python.

Original source