【问题标题】:How to map generic directory requests to the corresponding index.html file in Google AppEngine?如何将通用目录请求映射到 Google AppEngine 中相应的 index.html 文件?
【发布时间】:2018-10-31 05:24:07
【问题描述】:

我在 App Engine 中有一个使用默认 app.yaml 文件的静态站点。主 url 处理程序如下所示:

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

我想指导所有表格的请求:

http://mysite/somedirectoryhttp://mysite/somedirectory/

http://mysite/somedirectory/index.html

有什么好的方法吗?

【问题讨论】:

    标签: google-app-engine app.yaml


    【解决方案1】:

    对于http://mysite/somedirectory/ url 模式,您可以有这样的处理程序:

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

    当然,这个处理程序必须插入到你已经拥有的包罗万象的处理程序之前。

    但是对于http://mysite/somedirectory url 模式,事情很棘手,因为您无法判断somedirectory 是代表一个文件,应该按原样提供还是一个目录,在这种情况下,应该提供相应的index.html .

    除非您遵循可用于区分文件和目录的严格规则。例如,如果您知道 all 您的文件的名称中有 . 并且后面有一个文件扩展名,并且 all 目录的名称中没有 .,那么你可以像这样使用一对处理程序:

    # a "." in name means a file, serve as-is:
    - url: /(.*\/[^\/]*\.[^\/]*)$
      static_files: www/\1
      upload: www/.*
    
    # no "." in name means a directory, serve corresponding index.html:
    - url: /((.*))$
      static_files: www/\1/index.html
      upload: www/.*/index.html
    

    由于这对本身成为一个包罗万象的过滤器,它应该放在最后(你可能拥有的所有其他处理程序都应该放在它之前),替换你拥有的包罗万象的处理程序。

    【讨论】:

    • 谢谢,丹!我会尝试并报告。
    • 丹的回答让我走上了正轨。我会把我最终得到的答案作为新答案 - StackExchange 认为评论太长了。
    【解决方案2】:

    丹的回答让我走上了正轨。它适用于除根目录之外的所有内容。这就是我最终使用的:

    # Ending with a "." followed by non-slashes means a file, serve as-is:
    - url: /((.*\.[^\/]+))$
      static_files: www/\1
      upload: www/.*
    
    # Ending in a slash means a directory, serve corresponding index.html:
    - url: /((.*))/$
      static_files: www/\1/index.html
      upload: www/.*/index.html
    
    # Did not match above so must be a directory, serve corresponding index.html:
    - url: /((.*))$
      static_files: www/\1/index.html
      upload: www/.*/index.html
    

    您可以在https://www.tameware.com查看该网站的运行情况

    【讨论】:

    • 噢!这不适用于所有情况 - 我在发布之前没有进行充分测试。我会努力解决的。
    • 已修复和编辑。我在其中一个正则表达式中使用了单括号而不是双括号。
    • 还是有问题。所有这三个都将打开同一个页面,正如我想要的那样:mysite/somedirectorymysite/somedirectorymysite/somedirectory.index.html 但是如果我使用第一个链接导航,那么直接在该目录中单击访问链接将不起作用,给出 404。访问目录中的目录确实有效 - 好奇!
    • 如果我使用第一个链接导航到页面,那么当我尝试单击该目录中文件的链接时,服务器会在站点的根目录中查找它,即 mysite/而不是在 mysite/somedirectory/
    猜你喜欢
    • 2015-04-10
    • 2016-05-03
    • 2013-05-12
    • 1970-01-01
    • 2019-05-13
    • 2012-07-14
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多