Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect all URLs with Google App Engine

Tags:

How do I configure the app.yaml file to redirect all URLs to another URL? For example I want http://test.appspot.com/hello or http://test.appspot.com/hello28928723 to redirect to http://domain.com.

I am only serving static files at the moment. Here is my app.yaml file:

application: testapp version: 1 runtime: python api_version: 1  handlers: - url: (.*)/   static_files: static\1/index.html   upload: static/index.html  - url: /   static_dir: static 
like image 301
mistero Avatar asked Jun 29 '09 12:06

mistero


People also ask

Does Google crawl redirects?

Does Google crawl and index redirects? No, it does not. This means that if you redirect from one page to another, the content on the original page will not get indexed. Only the target URL will be crawled and indexed by the search engine.

How do I create a redirect URL?

Redirects allow you to forward the visitors of a specific URL to another page of your website. In Site Tools, you can add redirects by going to Domain > Redirects. Choose the desired domain, fill in the URL you want to redirect to another and add the URL of the new page destination. When ready, click Create.

How do I host a website with Google App Engine?

Go to the App Engine dashboard on the Google Cloud Platform Console and press the Create button. If you've not created a project before, you'll need to select whether you want to receive email updates or not, agree to the Terms of Service, and then you should be able to continue.

What is Google App Engine launcher?

Google App Engine (GAE) is a platform-as-a-service product that provides web app developers and enterprises with access to Google's scalable hosting and tier 1 internet service. GAE requires that applications be written in Java or Python, store data in Google Bigtable and use the Google query language.


1 Answers

Webapp2 has a built-in redirect handler

No need to roll your own handler; webapp2 already comes with one.

application = webapp2.WSGIApplication([     webapp2.Route('/hello', webapp2.RedirectHandler, defaults={'_uri':'http://domain.com'}),     webapp2.Route('/hello28928723', webapp2.RedirectHandler, defaults={'_uri':'http://domain.com'}), ], debug=False) 

The _uri argument is what the RedirectHandler class uses to define the destination. It took a lot of Google Fu to find the documentation on this but it works flawlessly in my app.

Update:

I assumed you're aware of this but you need to change your catch-all route from:

- url: /   static_dir: static 

To (python27 version):

- url: /.*   script: main.application 

Or: (pre python27 version)

- url: /.*   script: main.py 

main.py is the file containing the request handler + routes.

Note: There is no static-only way to handle redirects on GAE because of the nature of static files. Basically, there's no way to do a redirect in app.yaml alone.

like image 92
Evan Plaice Avatar answered Sep 24 '22 10:09

Evan Plaice