【问题标题】:Default document in Google App EngineGoogle App Engine 中的默认文档
【发布时间】:2016-05-30 17:31:05
【问题描述】:

是否可以为 Google App Engine 中的目录指定等效的默认文档,以便导航到该目录时自动重定向到它的 index.html,例如,如果它包含一个?

如果我的 app.yaml 中有这个:

- url: /demos
  static_dir: demos

并且 demos 目录包含一个 index.html 页面,我如何告诉 App Engine 自动重定向到该页面?

【问题讨论】:

  • 您应该澄清您在 GAE 上使用的编程语言。我会猜 Python 吗?

标签: google-app-engine


【解决方案1】:

App Engine 使用请求路径上的正则表达式匹配来确定要调用的脚本或要提供的文档。如果你只有一个索引文档,你可以这样做:

- url: /demos/
  static_files: demos/index.html
  upload: demos/index\.html

更一般地说,您可以为以斜杠(“目录”)结尾的路径定义静态文件,如下所示:

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

【讨论】:

  • 所以答案是“不是真的”,但这非常接近。
  • @dez 这与您的预期有何不同?
【解决方案2】:

我通过在我的 yaml 中使用它来实现它。

- url: /(.+)
  static_files: static/\1
  upload: static/(.+)

- url: /
  static_files: static/index.html
  upload: static/index.html

用演示替换静态,你应该设置。它将空白域重定向到 index.html 并将所有其他域重定向到静态文件夹。

【讨论】:

  • 耶!这在许多以前的尝试都没有的地方有效。谢谢!
【解决方案3】:

对于 GAE/J,将以下内容添加到您的 web.xml 文件中。

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

www.example.com/test/ 现在将服务于www.example.com/test/index.html

【讨论】:

  • 我相信 OP 正在询问如何在 python 项目中执行此操作。
【解决方案4】:
- url: /demos(/.+|)/
  static_files: demos\1/index.html
  upload: demos/(.+/|)index\.html
- url: /demos/
  static_dir: demos

【讨论】:

  • 我猜(/.+|)(管道是OR)和(/.+)?之间没有区别,后者对我来说似乎更清楚。我还使用了/.* 而不是/.+,所以用户可以输入奇怪的东西,比如/demos//
猜你喜欢
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
  • 2020-07-03
  • 1970-01-01
  • 2011-02-19
  • 2019-12-03
  • 1970-01-01
相关资源
最近更新 更多