Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect all requests from one domain to another with Google App Engine but keep static routing rules in yaml

I have a GAE app serving static files defined by rules in the yaml file under two different domain names as configured in DNS, an old one and a new one, but otherwise it's the same content served for each. I'd like to redirect requests from the old domain to the new domain. I've seen this question, but that loses the ability to use the static asset handlers in the yaml from what I can tell, and would have to set up static asset serving explicitly in my main.py I think. Is there a simple way (ideally in the yaml file itself) to do a redirect when the hostname is the old domain, but keep my static file rules in place for the new domain?

Update

Here's a complete solution that I ended up using:

### dispatch.yaml ###

dispatch:
- url: "*my.domain/*"
  module: redirect-module

### redirector.yaml ###

module: redirect-module
runtime: python27
threadsafe: true
api_version: 1

skip_files:
- ^(?!redirector.py$)

handlers:
# Redirect everything via our redirector
- url: /.*
  script: redirector.app

### redirector.py ###

import webapp2

def get_redirect_uri(handler, *args, **kwargs):
    return 'https://my.domain/' + kwargs.get('path')

app = webapp2.WSGIApplication([
    webapp2.Route('/<path:.*>', webapp2.RedirectHandler, defaults={'_uri': get_redirect_uri}),
], debug=False)

Some extra docs: https://cloud.google.com/appengine/docs/python/modules/routing#routing_with_a_dispatch_file

like image 824
qix Avatar asked Sep 19 '15 18:09

qix


1 Answers

AFAIK you can't do redirection for the static assets, since GAE serves them directly according to the .yaml file rules, without even hitting your app code.

You could add a module (let's call it redirect-module for example) to your app, route ALL old domain URLs to it using a dispatcher file and use a dynamic handler in this module to redirect URLs to the new domain equivalents, along the lines suggested in the answers to the question you referenced. The new domain requests will continue to work unmodified, served either as static assets or the existing module(s) of your app. The dispatch.yaml file would look like this:

application: your-app-name
dispatch:
  - url: "your.old.domain.com/*"
    module: redirect-module

Another thought that comes to mind (I didn't actually do this, so I'm unsure if it would address your problem) is to avoid the redirect altogether and instead of mapping your app to 2 different domains map it only to the new domain and make the old domain a DNS CNAME/alias to the new domain.

like image 118
Dan Cornilescu Avatar answered Nov 15 '22 10:11

Dan Cornilescu