【问题标题】:how to hide .html extension in URL from google app engine (cloud hosting)如何从谷歌应用引擎(云托管)隐藏 URL 中的 .html 扩展名
【发布时间】:2018-12-30 06:24:20
【问题描述】:

我使用谷歌应用引擎上传了我的网页,它运行良好。这是一个非常简单的网页,有 6 个静态文件(都是 .html)

我需要从 URL 中删除 .html 扩展名。 例如:我有 www.example.com/contact.html,我想要 www.example.com/contact

我当前的 app.yaml 是

runtime: php55
api_version: 1
threadsafe: true

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

- url: /
  static_dir: website

所有的 html 文件都在“网站”文件夹中,所以我怎样才能从 URL 中隐藏 .html 扩展名

所以请帮我解决它。

【问题讨论】:

标签: google-app-engine url url-rewriting web app.yaml


【解决方案1】:

如果您只有 6 个静态文件并且不想使用 .htaccess,则必须这样做:

代替

> handlers:
> 
> - url: /   static_dir: website

你可以:

handlers:

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

- url: /file1
  static_files: website/file1.html
  upload: website/file1.html

- url: /file2
  static_files: website/file2.html
  upload: website/file2.html

- url: /file3
  static_files: website/what_ever_name.what_ever_format
  upload: website/what_ever_name.what_ever_format

【讨论】:

  • 因为它对您有用,如果您接受答案以帮助社区快速找到有效的答案,我将不胜感激。
【解决方案2】:

您可以尝试这样的事情,但您不必依赖最后拥有的现有包罗万象的static_dir 处理程序,因为由于新的包罗万象处理程序扩展了任何其他带有.html 扩展名的文件模式:

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

# handler for specifically requested .html files
- url: /(.*\.html)$
  static_files: website/\1
  upload: website/.*\.html$

# add any other more specific file patterns here, before the catch-all case below

# catch-all handler implicitly serving .html files, no handler below it will be reached
- url: /(.*)$
  static_files: website/\1.html
  upload: website/.*\.html$

# no other handler from this point forward will be reached

附注:static_dirstatic_files 的重叠处理程序 url 模式可能会导致问题,请参阅 Static files are missing

【讨论】:

  • 当我使用上面的代码时,我得到一个ERROR: (gcloud.app.deploy) INVALID_ARGUMENT: The request is invalid. - '@type': type.googleapis.com/google.rpc.BadRequest fieldViolations: - description: 'Value "website/\1" must be a valid regular expression. Details : invalid escape sequence.' field: version.handlers[1].static_files.upload_path_regex - description: 'Value "website/\1.html" must be a valid regular expression. De tails: invalid escape sequence.' field: version.handlers[2].static_files.upload_path_regex,那么来自static_files: website/\11
  • 抱歉,我搞砸了upload 声明。我更新了答案
猜你喜欢
  • 2020-07-07
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 2014-07-31
  • 2012-11-25
  • 1970-01-01
  • 2011-01-08
  • 1970-01-01
相关资源
最近更新 更多