Redirect Domain On Google App Engine

Redirect to custom domain

Common use cases

  • Redirect to
  • Redirect (e.g. to
  • Redirect naked domain to

Redirect Naked Domain to www

This setup is the easiest without any coding involved.

At Google Cloud Console, goto App Engine > Settings > Custom Domains (or search Custom Domains at the top search bar).

Assuming you already setup custom domain for app engine and done your subdomain mapping correctly, you should be able to see this.

Custom Domain Mapping

If the above is configured correctly, it should redirect -> Else, refer to add custom domain to app engine to reconfigure again.

Sadly this method wouldn't work in redirecting from a subdomain to another subdomain. Both subdomains will show the same website without redirection taking place.

Redirect to custom domain

There are a few methods to achieve this, depending on your needs. These methods can also be used to redirect subdomains.

You might be wondering would it be possible to conigure domain redirection with app.yaml? The answer is no, as app.yaml only handles path redirection.

Redirect Request Handler

Create Python script to handle http requests. The script will listen to all requests to detect if the host is If if detected, it will redirect the request to the correct domain.

import webapp2class MainPage(webapp2.RequestHandler):    def get(self):        if''):            url = self.request.url.replace(, '')            # assume no SSL support yet            if url.startswith("https://"):                url = url.replace("https://", "http://")            return self.redirect(url, True)        else:            self.response.headers['Content-Type'] = 'text/plain'            self.response.write("Hello, World at {0}".format(self.request.path))app = webapp2.WSGIApplication([    ('/.*', MainPage),], debug=True)

Edit app.yaml to enable

runtime: python27api_version: 1threadsafe: truehandlers:- url: /.*  script:

This solution is suitable for most use cases, unless you don't plan to run server code to handle every requests (e.g. using App Engine to host static website). In that case, you can explore the following solutions.

Request routing with dispatch.yaml

Create an App Engine Service. Assuming you created a new project in hello-service directory and the service is named micro.

Go to the original application directory (not the newly created service), assuming created in the hello-app directory.
Create a dispatch file (dispatch.yaml). This service will handle all requests using the appspot domain.

dispatch:  - url: "*"    service: micro

Deploy the dispatch file.

gcloud app deploy dispatch.yaml# outputConfigurations to update:descriptor:      [/projects/hello-app/dispatch.yaml]type:            [routing rules]target project:  [projectid]

Now test the following:

  • Visit and you should see I am a micro service (The new service is working!)
  • Visit and you should see I am a micro service (The routing is working!)
  • Visit and you should see your original website (The routing is working perfectly!)

Server code 301 redirect

We need to configure the new micro service to redirect all requests to Goto hello-service directory and edit hello-service/

import webapp2class MainPage(webapp2.RequestHandler):    def get(self):        # do this to redirect all version and services: e.g.        # if''):        # do this to redirect the default service and version only        if == '':            url = self.request.url.replace(, '')            is_ssl = url.startswith("https://")            # assume no SSL support yet            if is_ssl:                url = url.replace("https://", "http://")            return self.redirect(url, True)        else:            self.response.headers['Content-Type'] = 'text/plain'            self.response.write("I am a micro service at {0}".format(self.request.path))app = webapp2.WSGIApplication([    ('/.*', MainPage),], debug=True)

Deploy the micro service.

# at hello-servicegcloud app deploy -v 1

Visit and it shall be redirected to

This solution of micro service is independent of the default application, but the drawbacks is higher instance cost. Whenever someone try to access, the App Engine will startup micro instance to handle the request (separate from the default instance). If more requests trigger the needs for redirection, the higher the instance cost shall be; if the request is low, the cost is negligible.

By default, the service will run Automatic Scaling (free quota of 28h per day). Instances are created on demand and automatically shutdown when idle. I would recommend the following app.yaml scaling settings to incur minimum cost.

# settings assuming extremely low usageautomatic_scaling:  min_idle_instances: 0  max_idle_instances: 1  min_pending_latency: 500ms  # default 30ms  max_pending_latency: 1000ms  max_concurrent_requests: 80 # default 8

HTML Redirect

Instead of redirecting using server code, you can perform HTML redirect using static HTML files instead using the above method.

Create index.html with the following content.

<!DOCTYPE HTML><html lang="en-US">  <head>    <meta charset="UTF-8">    <meta http-equiv="refresh" content="1; url=">    <script type="text/javascript">        window.location.href = ""    </script>    <title>Redirect: MyDomain</title>  </head>  <body>      If you are not redirected automatically, follow this <a href=''>link</a>.  </body></html>

Edit app.yaml with the following changes.

#handlers:#- url: /.*#  script: main.apphandlers:- url: /(.*)  static_files: index.html  upload: index.html

The disadvantage of this method is that it doesn't redirect with full original path. For example, will be redirected to (missing /test path).

Javascript redirect with full path

Edit index.html with the following content.

<!DOCTYPE HTML><html lang="en-US">  <head>    <meta charset="UTF-8">    <script type="text/javascript">        window.location.href = "" + (    </script>    <title>Redirect: MyDomain</title>  </head>  <body>    Redirecting ...     <noscript>      You browser doesn't support JavaScript, please follow this <a href=''>link</a>    </noscript>  </body></html>

Javascript allow redirecting with full original path and query string, though the browser needs to support and enable JavaScript.

Does serving static files save instance cost?

Why go through all the trouble to switch working Server Code to JavaScript and HTML for redirection purpose? I am guessing static contents like HTML on App Engine might not spin up an instance to handle these requests (as these contents are hosted on external more CDN-friendly servers). I tested multiple redirection using index.html on micro service and observed no instances being startup (Google Clound Engine > App Engine > Instances).

Conclusion: using HTML + Javascript for redirection would save instance cost.

Is JavaScript redirect SEO-friendly?

Basically Google suggest 301 redirects is the recommended ways to perform redirect. I assume JavaScript redirect stands pretty neutral in terms of SEO as long as it's not abused.

If this is a new project and has not been exposed to any search engine yet, then it's not necessary to perform any redirection. We just have to make sure no contents are served using, thus no pages shall be leaked to any search engine. We can use request routing with dispatch.yaml method to serve empty HTML for, and put it robots.txt to disable crawling.

If the pages of have been exposed to search engine for a long time or we are migrating to a different domain or subdomain, then using request routing with dispatch.yaml + server code 301 redirect is probably a good choice. If the cost of using App Engine redirect is too high, we can consider hosting a cheap server to perform the redirect (I wonder is there a service or product which handle redirection of domain pages).

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.