PHP 5 已达到支持终止期限,并将于 2026 年 1 月 31 日
弃用。弃用后,您将无法部署 PHP 5 应用,即使您的组织之前曾使用组织政策重新启用旧版运行时的部署也是如此。现有的 PHP 5 应用在
弃用日期之后将继续运行并接收流量。我们建议您
迁移到最新支持的 PHP 版本。
模拟 Apache mod_rewrite 路由
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
通过使用从 app.yaml
中引用的 PHP 脚本,您可以模拟基本的 Apache mod_rewrite
功能,该文件将依次加载所需的脚本。此示例将模拟要求变量 $_GET['q']
包含请求路径的常见 PHP 模式。
mod_rewrite.php
简介
要启用 mod_rewrite 功能,您的应用需要包含将针对应用的所有请求调用的 mod_rewrite.php
脚本,以便执行请求路由。如注释中所述,脚本将检查是否存在根级 PHP 脚本,并在调用这些脚本的同时将 $_SERVER['REQUEST_URI']
路径部分放入 $_GET['q']
变量。
<?php
/**
* @file
* Provide basic mod_rewrite like functionality.
*
* Pass through requests for root php files and forward all other requests to
* index.php with $_GET['q'] equal to path. The following are examples that
* demonstrate how a request using mod_rewrite.php will appear to a PHP script.
*
* - /install.php: install.php
* - /update.php?op=info: update.php?op=info
* - /foo/bar: index.php?q=/foo/bar
* - /: index.php?q=/
*/
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// Provide mod_rewrite like functionality. If a php file in the root directory
// is explicitly requested then load the file, otherwise load index.php and
// set get variable 'q' to $_SERVER['REQUEST_URI'].
if (dirname($path) == '/' && pathinfo($path, PATHINFO_EXTENSION) == 'php') {
$file = pathinfo($path, PATHINFO_BASENAME);
}
else {
$file = 'index.php';
// Provide mod_rewrite like functionality by using the path which excludes
// any other part of the request query (ie. ignores ?foo=bar).
$_GET['q'] = $path;
}
// Override the script name to simulate the behavior without mod_rewrite.php.
// Ensure that $_SERVER['SCRIPT_NAME'] always begins with a / to be consistent
// with HTTP request and the value that is normally provided.
$_SERVER['SCRIPT_NAME'] = '/' . $file;
require $file;
示例应用
下面显示了一个非常简单的应用,其编写目的在于获取 $_GET['q']
。
app.yaml
从该 app.yaml
文件中可以看出,此应用将提供两个根级 PHP 脚本,并从名为 downloads
的目录中传送静态文件。
application: mod_rewrite_simulator
version: 1
runtime: php55
api_version: 1
handlers:
# Example of handler which should be placed above the catch-all handler.
- url: /downloads
static_dir: downloads
# Catch all unhandled requests and pass to mod_rewrite.php which will simulate
# mod_rewrite by forwarding the requests to index.php?q=... (or other root-level
# PHP file if specified in incoming URL.
- url: /.*
script: mod_rewrite.php
index.php
index.php
是路由器样式的 index.php
,它读取 $_GET['q']
来确定请求路径。
<?php
if ($_GET['q'] == '/help') {
echo 'This is some help text.';
exit;
}
echo 'Welcome to the site!';
other.php
以下是可以直接调用的根级脚本的示例。许多 PHP 框架都有 install.php
或 update.php
等脚本实现类似功能。
<?php
echo 'Welcome to the other site.';
示例请求
参考上方的示例应用,以下请求将作如下处理。
/
使用 $_GET['q'] = '/'
解析为 index.php
/help
使用 $_GET['q'] = '/help'
解析为 index.php
/other.php
使用 $_GET['q'] = null
解析为 other.php
/downloads/foo_17.png
解析为 downloads/foo_17.png
避免使用 mod_rewrite.php
许多 PHP 框架不再依赖于 $_GET['q']
。这些框架改用 $_SERVER['REQUEST_URI']
,该语法无需借助 mod_rewrite
发挥作用。因此后者是 App Engine 上的首选方法。
在 mod_rewrite.php
中,利用 REQUEST_URI
的简单方法如下:
<?php
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if ($path == '/help') {
echo 'This is some help text.';
exit;
}
echo 'Welcome to the site!';
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-04。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\u003cp\u003e\u003ccode\u003emod_rewrite\u003c/code\u003e functionality can be simulated in PHP by using a script (\u003ccode\u003emod_rewrite.php\u003c/code\u003e) that routes requests based on the URL path.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003emod_rewrite.php\u003c/code\u003e script checks for root-level PHP scripts and invokes them, otherwise forwarding requests to \u003ccode\u003eindex.php\u003c/code\u003e with the path portion in the \u003ccode\u003e$_GET['q']\u003c/code\u003e variable.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eapp.yaml\u003c/code\u003e file defines how requests are handled, routing specific paths to static directories or passing unhandled requests to \u003ccode\u003emod_rewrite.php\u003c/code\u003e for processing.\u003c/p\u003e\n"],["\u003cp\u003ePHP frameworks that utilize \u003ccode\u003e$_SERVER['REQUEST_URI']\u003c/code\u003e directly are preferred on App Engine, eliminating the need for \u003ccode\u003emod_rewrite.php\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eAn example application showcases how to handle requests with root-level scripts and static file directories.\u003c/p\u003e\n"]]],[],null,["# Simulate Apache mod_rewrite routing\n\nBasic [Apache\n`mod_rewrite`](http://httpd.apache.org/docs/current/mod/mod_rewrite.html) functionality can be simulated through the use of a PHP script\nreferenced from `app.yaml` that will in turn load the desired script. This example\nsimulates the common PHP pattern that expects the variable `$_GET['q']` to contain the\nrequest path.\n\nAbout `mod_rewrite.php`\n-----------------------\n\nTo enable mod_rewrite functionality, your application needs to include\n`mod_rewrite.php`, which is the script that will be invoked for all requests to your\napplication to perform the request routing. As described in the comments the script will check for\nthe existence of root-level PHP scripts and invoke them while placing the path portion of\n`$_SERVER['REQUEST_URI']` in the `$_GET['q']` variable. \n\n```php\n\u003c?php\n/**\n * @file\n * Provide basic mod_rewrite like functionality.\n *\n * Pass through requests for root php files and forward all other requests to\n * index.php with $_GET['q'] equal to path. The following are examples that\n * demonstrate how a request using mod_rewrite.php will appear to a PHP script.\n *\n * - /install.php: install.php\n * - /update.php?op=info: update.php?op=info\n * - /foo/bar: index.php?q=/foo/bar\n * - /: index.php?q=/\n */\n\n$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);\n\n// Provide mod_rewrite like functionality. If a php file in the root directory\n// is explicitly requested then load the file, otherwise load index.php and\n// set get variable 'q' to $_SERVER['REQUEST_URI'].\nif (dirname($path) == '/' && pathinfo($path, PATHINFO_EXTENSION) == 'php') {\n $file = pathinfo($path, PATHINFO_BASENAME);\n}\nelse {\n $file = 'index.php';\n\n // Provide mod_rewrite like functionality by using the path which excludes\n // any other part of the request query (ie. ignores ?foo=bar).\n $_GET['q'] = $path;\n}\n\n// Override the script name to simulate the behavior without mod_rewrite.php.\n// Ensure that $_SERVER['SCRIPT_NAME'] always begins with a / to be consistent\n// with HTTP request and the value that is normally provided.\n$_SERVER['SCRIPT_NAME'] = '/' . $file;\nrequire $file;\n```\n\nExample app\n-----------\n\nThe following show a very simple application written to expect `$_GET['q']`.\n\n### `app.yaml`\n\nAs you can see from this `app.yaml` file, this application will provide two root-level\nPHP scripts and serve static files out of a directory named `downloads`. \n\n```yaml\napplication: mod_rewrite_simulator\nversion: 1\nruntime: php55\napi_version: 1\n\nhandlers:\n# Example of handler which should be placed above the catch-all handler.\n- url: /downloads\n static_dir: downloads\n\n# Catch all unhandled requests and pass to mod_rewrite.php which will simulate\n# mod_rewrite by forwarding the requests to index.php?q=... (or other root-level\n# PHP file if specified in incoming URL.\n- url: /.*\n script: mod_rewrite.php\n```\n\n### `index.php`\n\n`index.php` is a router-style `index.php` which reads\n`$_GET['q']` to determine the request path. \n\n```php\n\u003c?php\n\nif ($_GET['q'] == '/help') {\n echo 'This is some help text.';\n exit;\n}\n\necho 'Welcome to the site!';\n```\n\n### `other.php`\n\nThe following is an example of a root-level script that can be called directly. Many\nPHP frameworks have scripts like `install.php` or `update.php` that would\nbehave similarly. \n\n```php\n\u003c?php\n\necho 'Welcome to the other site.';\n```\n\n### Example requests\n\nGiven the above example application the following requests would be handled as shown.\n\n- `/` translates to `index.php` with `$_GET['q'] = '/'`\n- `/help` translates to `index.php` with `$_GET['q'] = '/help'\n `\n- `/other.php` translates to `other.php` with `$_GET['q'] = null\n `\n- `/downloads/foo_17.png` translates to `downloads/foo_17.png`\n\nAvoid the need for `mod_rewrite.php`\n------------------------------------\n\nMany PHP frameworks no longer depend on `$_GET['q']`. Instead they make use of\n`$_SERVER['REQUEST_URI']` which works both with and without `mod_rewrite`.\nAs such the latter is the preferred method on App Engine.\n\nAs used in `mod_rewrite.php` a clean method of utilizing `REQUEST_URI` is\nthe following: \n\n```php\n\u003c?php\n\n$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);\n\nif ($path == '/help') {\n echo 'This is some help text.';\n exit;\n}\n\necho 'Welcome to the site!';\n```"]]