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

拥有回忆 提交于 2020-12-05 11:33:05

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/32671890/redirect-all-requests-from-one-domain-to-another-with-google-app-engine-but-keep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!