PHP 5 已終止支援,並將於 2026 年 1 月 31 日
淘汰。淘汰後,您將無法部署 PHP 5 應用程式,即使貴機構先前曾使用機構政策重新啟用舊版執行階段的部署作業,也無法部署。現有的 PHP 5 應用程式在
淘汰日期後仍會繼續執行並接收流量。建議您
改用系統支援的最新 PHP 版本。
模擬 Apache mod_rewrite 轉送
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以使用從 app.yaml
參照的 PHP 指令碼模擬基本 Apache mod_rewrite
功能,該指令碼會依序載入所需指令碼。本範例模擬常見的 PHP 模式;此模式預期 $_GET['q']
變數會包含要求路徑。
關於「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!';
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間: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"]],["上次更新時間: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```"]]