如何配置Google App Engine yaml文件以处理404错误 [英] How to configure Google App Engine yaml file to handle 404 Error

查看:89
本文介绍了如何配置Google App Engine yaml文件以处理404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要将所有404链接重定向到www文件夹内的index.html

这是我的app.yaml

This is my app.yaml

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /
  static_files: www/index.html
  upload: www/index.html

- url: /(.*)
  static_files: www/\1
  upload: www/(.*)

这是一个静态的angular 2应用程序,我需要将所有未找到的404错误定向到index.html. 有(www)文件夹,并且所有文件都在其中,其中包括index.html.

It's a static angular 2 app , and i need to direct all page not found 404 errors to index.html. There is (www) folder and inside that all file including index.html there.

推荐答案

因此,将其添加为最后一条规则,将在所有其他规则均失败的情况下将其用作index.html

So adding this as the last rule, will cause it to serve index.html if all other rules fail

- url: /.*
  static_files: www/index.html
  upload: www/(.*)

但是我认为您想要的是实际执行重定向.否则,您的基本网址仍将是一些虚假网址.您需要在服务器代码中设置一个基本的请求处理程序来完成此操作(在您的情况下,您的服务器运行时为python27).

But I think what you want is for it to actually perform a redirect; otherwise, your base url will still be some bogus url. You need to setup a basic request handler in server code to do this right (and in your case your server runtime is python27).

因此将此规则添加到app.yaml

- url: /.*
  script: main.app

然后添加一个名为main.py的文件,其中包含以下内容:

And then add a file called main.py with something like this in it:

import webapp2
app = webapp2.WSGIApplication()

class RedirectToHome(webapp2.RequestHandler):
    def get(self, path):
        self.redirect('/www/index.html')


routes = [
    RedirectRoute('/<path:.*>', RedirectToHome),
]

for r in routes:
    app.router.add(r)

这篇关于如何配置Google App Engine yaml文件以处理404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆