6

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

Community
  • 1
  • 1
qix
  • 6,008
  • 1
  • 44
  • 58
  • Does this still work? I am not able to get it working on my domain – Alok Kumar Oct 05 '17 at 09:45
  • @AlokKumar what sort of errors are you getting? Are you not getting the expected http status code when requesting a url from the old domain, and if not what do you see? (Looks like webapp2 was updated since I originally wrote this, although there's no obvious related breakages: https://github.com/GoogleCloudPlatform/webapp2/blob/master/CHANGES) Or have you investigated whether it's an issue with the yaml rules? Certainly Google could have changed the processing of it in the past 2 years. – qix Oct 16 '17 at 16:33
  • I dont get any error. What I wanted was that if anyone types www link it should redirect to non-www link. I have setup same thing. You can check it here https://www.cloudnowtech.com I guess the redirect module is working because when I deployed a new version by removing redirect-module from yaml it updated only the default instance and www still showed old version. So redirect modules works but I want the URL to be changed – Alok Kumar Oct 19 '17 at 06:52

1 Answers1

4

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.

Dan Cornilescu
  • 37,297
  • 11
  • 54
  • 89
  • Thanks for the suggestions! I don't believe the DNS route works for me because of the need to support SSL on both, but thanks for confirming my suspicions and the example of how to work around this. – qix Sep 22 '15 at 16:46
  • I've very new to GAE, could you just elaborate, where to write down this redirect module? I'm struggling to redirect my www to naked domain. – anshulix May 13 '16 at 16:13
  • @anshulix: do you actually **need** that redirect? (an unnecessary extra redirection isn't something nice). Isn't just mapping the app to both `www` and naked domains working for you? See http://stackoverflow.com/questions/34826320/google-cloud-dns-point-naked-domain-to-www/34830367#34830367 – Dan Cornilescu May 13 '16 at 16:54
  • @Dan: I'm working on service workers, wherein if my users get exposed to naked and sub-domain it causes conflict in app behavior. Either I want to redirect www to naked or all naked domain to www. – anshulix May 14 '16 at 07:18
  • Thanks for the link, your solution to it, I already did. Still users are able to use both www and naked url. Stuck for too long on this thing now :( – anshulix May 14 '16 at 08:03
  • Got it. This is probably what you're after: http://stackoverflow.com/questions/1364733/block-requests-from-appspot-com-and-force-custom-domain-in-google-app-engine/1980033#1980033 – Dan Cornilescu May 14 '16 at 12:34