How to Regex for static webpage on Google App Engi

2019-09-03 07:31发布

问题:

I have a Static site I wish to upload to Google Apps, I am having trouble with the Regex side and would like some help, either explaining what I've done wrong or pointing me to resource which may help.

Below is what I've done so far:

Site structure

  • my-static-site (not my real app name)
    • dist
      • about
      • blog
        • post-1
          • index.html
      • images
      • project
        • project-1
          • index.html
      • scripts
      • styles
      • about.html
      • archives.html
      • index.html
      • project.html

app.yaml code

application: my-static-site
version: 1
runtime: python27
api_version: 1
threadsafe: no

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

- url: /
  static_dir: dist

# to load my-static-site.appspot.com/project/project-1
- url: /project/project-1/
  static_files: dist/project/project-1/index.html
  upload: dist/project/project-1/index.html

# to load my-static-site.appspot.com/blog/post-1
- url: /blog/post-1/
  static_files: dist/blog/post-1/index.html
  upload: dist/blog/post-1/index.html

How this behaves

Going to my-static-site.appspot.com shows the correct home page with styling, scripts etc.

Clicking on the served 'post 1' link in the blog section redirects to my-static-site.appspot.com/blog/post-1/ and presents a blank screen, so the index.html is not being shown here. This is the same for the 'project 1' link in the project section.

However, if i manually append index.html to either the project of blog url, the content is served. E.g. my-static-site.appspot.com/blog/post-1/index.html shows correctly.

What am I doing wrong?

Thank you!

回答1:

Played around and found order matters in Regex, this is what works:

application: my-static-site
version: 2
runtime: python27
api_version: 1
threadsafe: no

handlers:
- url: /(.+)/(.+)/
  static_files: dist/\1/\2/index.html
  upload: dist/(.+)/(.+)/index.html

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

- url: /
  static_dir: dist

Locally the blog posts and project posts with url /blog/post-1 and /project/project-1 serve the index.html file in each dir.

If anyone has a more elegant solution feel free to chime in.