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
는 $_GET['q']
를 읽어 요청 경로를 결정하는 라우터 스타일의 index.php
입니다.
<?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']
에 종속되지 않습니다. 대신 mod_rewrite
가 있는지 여부에 상관없이 작동하는 $_SERVER['REQUEST_URI']
를 사용합니다.
따라서 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!';
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 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```"]]