PHP 5 はサポートが終了しており、2026 年 1 月 31 日に
非推奨になります。非推奨になると、過去に組織のポリシーを使用して以前のランタイムのデプロイを再度有効にしていた場合でも、PHP 5 アプリケーションをデプロイできなくなります。既存の PHP 5 アプリケーションは、
非推奨になる日付以降も引き続き実行され、トラフィックを受信します。
サポートされている最新バージョンの PHP に移行することをおすすめします。
Apache mod_rewrite ルーティングをシミュレートする
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
基本的な Apache mod_rewrite
機能は、app.yaml
から参照される PHP スクリプトを使用してシミュレートできます。その際に、目的のスクリプトが読み込まれます。この例では、変数 $_GET['q']
にリクエストパスが含まれることが予期される一般的な PHP パターンをシミュレートします。
mod_rewrite.php
の概要
mod_rewrite 機能を有効にするには、アプリケーションに mod_rewrite.php
を含めます。アプリケーションに対してリクエストが送信されると、このスクリプトが呼び出され、リクエストのルーティングを実行します。コメントに記述されているように、このスクリプトはルートレベルに PHP スクリプトがあるかどうかを確認します。スクリプトが見つかると、$_GET['q']
変数の $_SERVER['REQUEST_URI']
のパス部分を置き換え、スクリプトを呼び出します。
<?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
ファイルに記述されているように、このアプリケーションにはルートレベルに 2 つの 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 Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-09-04 UTC。
[[["わかりやすい","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 UTC。"],[[["\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```"]]