Google App Engine app.yaml match all PHP files and match all HTML files and serve respectively

ⅰ亾dé卋堺 提交于 2020-01-01 19:17:27

问题


I'm using Google App Engine with PHP. I want all HTML files in the folder Client_Pages to be recognized and served as static files, and all the PHP files in the same folder to be recognized and served as script files. Here is what the app.yaml file looks like:

application: myappname
version: 1
runtime: php
api_version: 1
threadsafe: true

handlers:
- url: /
  script: index.php

- url: /Client_Pages/*.php
  script: /Client_Pages/*.php

- url: /Client_Pages/*.html
  static_dir: Client_Pages/*.html

I guess the asterisk doesn't work as a wildcard in app.yaml? I've tried using:

- url: /(.+\.php)
  script: Client_Pages/\1

But that doesn't work. I don't even know what all the symbols mean, so I'm just hacking around, hoping something will work. I don't want to do that, but I can't find documentation for what all those symbols do.

This will work for a specific page:

- url: /Client_Pages/InputForm.php
  script: /Client_Pages/InputForm.php

I could put a line in my app.yaml file for every single page in my website, but I'd rather find a different way.

Oh wait! Hold on! This works for loading my PHP files:

- url: /(.+\.php)
  script: \1

So what does the \1 mean? Or where is a good reference?

Okay, so the above works for php pages, but this will NOT work for HTML pages, why?

- url: /(.+\.html)
  static_dir: \1

I got something to work. This will load both my PHP and HTML pages:

- url: /(.+\.php)
  script: \1

- url: /Client_Pages
  static_dir: Client_Pages

So I found a solution, but I still have absolutely no idea why it works, or what the symbols mean.

I apologize for finding a partial answer to my own question in the process of posting it, but if someone could explain why it works, I'll give you the points you deserve.


回答1:


The python regular expression reference is probably easiest to follow and closest to what's happening here, along with the app.yaml reference.

Essentially, the ( ) in the regular expression specify a grouping (also known as back referencing), and your regular expression can have many groups. The \1 is a reference to the first group, \2 would be the second group etc etc. This lets you extract out the value that matched the regular expression and use that value for selecting the script or static resource.

So basically for the regular expression

/(.+\.php) 

is saying 'make the first group any value that matches the regular expression .+\.php'. The value that matches the regular expression can be retrieved by using the \1 which specified the first group.

So the value /foobar.php matches the regular expression .+.php and the value of \1 becomes foobar.php because of the ( ) around the regular expression.

Slightly more complicated:

/(.+)/(.+)\.php$

The value of /foo/bar.php would match this regular expression, \1 would equal 'foo' [as it's the first group in the regular expression] and \2 would equal 'bar' [as it's the second group].

Note that this regular expression would not match /foobar.php.

You could also do something like this so that the URL doesn't need to have the .php in it.

- url: /(.+)
  script: \1.php

By using static_dir your removing the need for grouping, as it will match to any file that is in the directory.

If you wanted to match just the *.html files you could use

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

Finally, the thing to remember with app.yaml as it processes the rules from top to bottom, and the first match is executed and processing stops.

So an app.yaml like

- url: /.*
  script: index.php

- url: /(.+)\.html
  static_dir: html_pages

would always execute index.php as the first regular expression would match any incomming URL.



来源:https://stackoverflow.com/questions/22184211/google-app-engine-app-yaml-match-all-php-files-and-match-all-html-files-and-serv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!