【问题标题】:How to configure Google App Engine yaml file to handle 404 Error如何配置 Google App Engine yaml 文件以处理 404 错误
【发布时间】:2018-10-17 14:08:17
【问题描述】:

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

这是我的 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 在内的所有文件。

【问题讨论】:

  • 这是否适用于 PathLoactionStrategy?

标签: angular google-app-engine google-cloud-platform angular2-routing


【解决方案1】:

因此,将其添加为最后一条规则,如果所有其他规则都失败,它将使其服务index.html

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

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

所以将此规则添加到app.yaml

- url: /.*
  script: main.app

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

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)

【讨论】:

    猜你喜欢
    • 2019-05-24
    • 2012-11-19
    • 2015-12-08
    • 2019-04-06
    • 2015-05-10
    • 2015-02-10
    • 2010-09-16
    • 2022-01-24
    • 1970-01-01
    相关资源
    最近更新 更多