Die grundlegende mod_rewrite-Funktionalität von Apache kann mit einem PHP-Skript simuliert werden, auf das von app.yaml verwiesen wird, die wiederum das gewünschte Skript lädt. In diesem Beispiel wird das allgemeine PHP-Muster simuliert. Dabei wird davon ausgegangen, dass die Variable $_GET['q'] den Anfragepfad enthält.
Über mod_rewrite.php
Zur Aktivierung der mod_rewrite-Funktionalität muss Ihre Anwendung mod_rewrite.php enthalten. Dieses Skript wird für alle Anfragen an Ihre Anwendung für das Anfragenrouting aufgerufen. Wie in den Kommentaren beschrieben, prüft das Skript, ob PHP-Skripts auf Stammebene vorhanden sind. Ist dies der Fall, ruft es diese Skripts auf und platziert den Pfadabschnitt von $_SERVER['REQUEST_URI'] in der Variable $_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;
Beispielanwendung
Das folgende Beispiel zeigt eine sehr einfache Anwendung, die $_GET['q'] erwartet.
app.yaml
Wie Sie in dieser Datei app.yaml sehen können, stellt diese Anwendung zwei PHP-Skripts auf Stammebene bereit und übergibt statische Dateien aus einem Verzeichnis namens 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 ist eine index.php-Datei im Routerstil, die aus $_GET['q'] den Anfragepfad ermittelt.
<?php if ($_GET['q'] == '/help') { echo 'This is some help text.'; exit; } echo 'Welcome to the site!';
other.php
Das folgende Beispiel zeigt ein Skript auf Stammebene, das direkt aufgerufen werden kann. Viele PHP-Frameworks haben Skripts wie install.php oder update.php, die sich ähnlich verhalten würden.
<?php echo 'Welcome to the other site.';
Beispielanfragen
Mit der obigen Beispielanwendung werden die folgenden Anfragen wie angegeben verarbeitet.
/wird alsindex.phpmit$_GET['q'] = '/'gelesen./helpwird alsindex.phpmit$_GET['q'] = '/help'gelesen./other.phpwird alsother.phpmit$_GET['q'] = nullgelesen./downloads/foo_17.pngwird alsdownloads/foo_17.pnggelesen.
Notwendigkeit von mod_rewrite.php vermeiden
Viele PHP-Frameworks hängen nicht mehr von $_GET['q'] ab. Stattdessen verwenden sie $_SERVER['REQUEST_URI'], das sowohl mit als auch ohne mod_rewrite funktioniert.
Dies ist daher die bevorzugte Methode in App Engine.
Im Folgenden ist das Beispiel einer eindeutigen Methode zur Verwendung von REQUEST_URI in mod_rewrite.php dargestellt:
<?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!';