Google App Engine app.yaml会匹配所有PHP文件并匹配所有HTML文件并分别提供服务 [英] Google App Engine app.yaml match all PHP files and match all HTML files and serve respectively

查看:301
本文介绍了Google App Engine app.yaml会匹配所有PHP文件并匹配所有HTML文件并分别提供服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google App Engine和PHP。我想要识别文件夹 Client_Pages 中的所有 HTML 文件并将其作为静态文件提供,并且所有 PHP 文件在同一文件夹中被识别并作为脚本文件。以下是 app.yaml 文件的样子:

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

我猜星号 app.yaml 中不能用作通配符。我尝试过使用:

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.

这适用于特定页面:

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

我可以在我的 app.yaml 文件中为我的每一页网站,但我宁愿找到一种不同的方式。

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.

哦,等等!等一下!这适用于加载我的PHP文件:

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

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

那么 \ 1 是什么意思?或者是一个很好的参考?

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

好的,所以上述工作适用于 php 页面,但这将 适用于HTML网页,为什么?

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

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

我得到了一些工作。这会加载我的 PHP HTML 页面:

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.

推荐答案

python 正则表达式引用可能最容易遵循并且最接近这里发生的事情,以及 app.yaml 参考。

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

基本上,正则表达式中的()指定一个分组(也被称为后退引用),而你的正则表达式可以有很多组。 \1是对第一组的引用,\\ 2将是第二组等等。这可以让您提取与正则表达式匹配的值,并使用该值来选择脚本或静态资源。

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) 

表示'使第一组的任何值与正则表达式匹配+ \.php 。与正则表达式相匹配的值可以通过使用指定第一个组的\ 1来检索。

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.

.php和\1的值由于正则表达式中的()而变为foobar.php。

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.

稍微复杂一点:

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

/foo/bar.php的值将与此正则表达式匹配,\ 1将等于'foo'[因为它是正则表达式中的第一个组],并且\ 2将等于'bar'[因为它是第二组]。

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].

请注意,这个正则表达式不会匹配/foobar.php。

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

你也可以做这样的事情,这样URL就不需要有.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

通过使用static_dir可以消除分组的需要,因为它会匹配任何文件都在目录中。

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

如果您只想匹配* .html文件,您可以使用

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$

最后,要记住app.yaml,因为它处理从顶部到底部,并执行第一个匹配并停止处理。

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.

所以一个app.yaml就像

So an app.yaml like

- url: /.*
  script: index.php

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

始终执行index.php,因为第一个正则表达式可以匹配任何包含的网址。

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

这篇关于Google App Engine app.yaml会匹配所有PHP文件并匹配所有HTML文件并分别提供服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆