Routing
The web server selects the PHP controller before PHP starts handling the request. Choose config/nginx.conf, included inside Nginx's server, or config/apache.conf, included inside Apache's <VirtualHost>. The file contains routes and script protection; the environment owns the document root, TLS and common PHP handler. Apache uses mod_rewrite in virtual-host context, not .htaccess, so neither profile reads a routing file on each request.
On this page
Required environment
Use PHP 8.3–8.6 with Nginx/PHP-FPM or Apache 2.4 with mod_rewrite and a PHP handler. The hosting environment supplies:
- A document root pointing to the application's
web/directory. - An include of the chosen application routing file directly inside Nginx's
serveror Apache's<VirtualHost>, using the project's absolute path. - A common PHP handler that executes the selected script and passes the rewritten query string. Nginx's handler must check that the script exists and supply its filename through
SCRIPT_FILENAMEwith the standard FastCGI parameters. The Apache routing fragment performs the existence check before dispatch.
Nginx:
include /srv/my-app/config/nginx.conf;
Apache:
Include /srv/my-app/config/apache.conf
Apache examples use virtual-host rules, not .htaccess or <Directory> rules. The small application also needs Composer's autoloader and the shared initialization below. Hello world needs neither. Installation, PHP integration and server validation/reload remain responsibilities of the hosting environment.
When checking the examples, an unexpected 404 calls for checking the route include and selected method file; with Nginx/PHP-FPM, a 502 calls for checking the PHP-FPM connection. If PHP runs but cannot find an application class, check composer install, the namespace mapping and the configured bootstrap path.
Select a controller by method
Nginx:
rewrite ^/articles$ /$handler_method/articles.php last;
Apache:
RewriteRule ^/articles$ /%{ENV:handler_method}/articles.php
GET /articles selects web/GET/articles.php. Creating web/POST/articles.php enables POST through the same route. A missing method file receives 404. HEAD selects the GET file while PHP still receives HEAD as the request method; the server omits the response body. The selection helpers shown below belong before the endpoint rewrites.
last asks Nginx to select a location for the rewritten URI. Its common PHP location passes the selected script to PHP-FPM. Apache rules continue to the shared dispatch block shown below, which checks the file before handing the rewritten URI to the PHP handler. Do not add [L] or [PT] to individual Apache endpoint rules: they would bypass that final check. No PHP-handler settings need to be repeated per route.
The server reads configuration at startup or reload and matches requests against it thereafter. Adding or changing a rewrite requires a reload; PHP does not reread a routing file on each request.
Derive the endpoint filename
Start with the complete route schema, then preserve its literal segments and replace each parameter by its lowerCamelCase name in square brackets. Append .php to the last segment and prefix web/ and the uppercase method; HEAD uses GET. The brackets are literal filename characters, never request values.
| Route schema | GET endpoint |
|---|---|
/ |
web/GET/index.php |
/articles |
web/GET/articles.php |
/articles/{id} |
web/GET/articles/[id].php |
/articles/new/{page?} |
web/GET/articles/new/[page].php |
/images/{id}.jpg |
web/GET/images/[id].jpg.php |
/{tag}.{format} |
web/GET/[tag].[format].php |
Optional parameters stay in the filename: /articles/new and /articles/new/2, when variants of /articles/new/{page?}, select the same file. Defaults belong in the controller. Preserve literal case, punctuation and extensions. A trailing slash is a variant or redirect of the same route. Reserve index.php for /; /index needs an explicit alias or a project-specific exception.
For a finite choice used by none of the route's methods, use _ for that segment: /{kind}/{id}/like, with kind restricted to book or film, can select web/POST/_/[id]/like.php. If any method needs the choice, use [kind] for every method. An ordinary unused parameter, such as a decorative slug, keeps its brackets.
The native configuration remains authoritative for parameter names, constraints, optionality and aliases. A filename alone cannot recover that information and does not register a route. Quote paths containing brackets in shell commands, for example php -l 'web/GET/articles/[id].php'.
Pass a path parameter
Nginx:
rewrite ^/articles/(\d+)$ /$handler_method/articles/[id].php?id=$1? last;
Apache:
RewriteRule ^/articles/(\d+)$ /%{ENV:handler_method}/articles/[id].php?id=$1
Both servers capture the digits in /articles/12 and supply them as $_GET["id"]. Nginx needs the terminal ? to discard the original query string. Apache replaces it when the substitution contains a query string; do not add QSA, which would combine the two. Thus /articles/12?id=99 supplies only id=12 in both profiles. Other incoming query parameters are discarded on this detail route. Rules without a query in their substitution retain the incoming query string.
The controller reads the value directly:
<?php
$id = $_GET["id"];
No PHP regular expression or path splitting is needed. The value is a string of digits; the application example uses it as a string identifier. If your application requires an integer, validate its range before converting it: a sequence of digits can exceed PHP's integer range.
Use this query-binding technique for digits or URL-safe slugs; arbitrary text needs an encoding contract and is outside this example.
Protect implementation files
Only web/ is public. Keep source classes, dependencies and configuration outside it. For Nginx, place the script guard before the rewrites and supply a fallback for unknown URLs:
if ($uri ~ "\.php(?:/|$)") {
return 404;
}
location / { return 404; }
set $handler_method $request_method;
if ($request_method = HEAD) {
set $handler_method GET;
}
The server-level guard runs before the rewrites. Their last location lookup reaches the common PHP handler without repeating that guard. Keep the handler at the same server level and check the complete configuration through your environment.
For Apache, start the routing file with:
RewriteEngine On
RewriteRule \.php(?:/|$) - [R=404,L]
RewriteRule ^ - [E=handler_method:%{REQUEST_METHOD}]
RewriteCond %{REQUEST_METHOD} ^HEAD$
RewriteRule ^ - [E=handler_method:GET]
Place endpoint rules next, then finish the file with this shared dispatch block:
RewriteCond %{DOCUMENT_ROOT}$0 -f
RewriteRule ^/.*\.php$ - [PT]
RewriteRule ^ - [R=404,L]
$0 is the whole path matched by the following rule, after the endpoint rewrite. The condition checks that the selected PHP file exists under the document root. [PT] then passes that URI to the normal handler and stops rewriting. Otherwise, the last rule returns 404, including when the method directory does not exist. This prevents the PHP handler's missing-file behavior from determining the response. The Apache flags reference explains PT, R and query-string handling.
For a local redirect, use rewrite ^/$ /articles redirect; with Nginx or RewriteRule ^/$ /articles [R=302,L] with Apache. Apache redirects stop before the dispatch block.
A missing resource is handled by the controller with http_response_code(404); exit;. A URL that matches no route is handled by the server.
Shared initialization
The larger example uses a bootstrap.php to load Composer's autoloader. The environment sets PHP's auto_prepend_file to that file's absolute path, so it runs before each controller. The bootstrap initializes PHP; it does not choose a controller.
The environment configures PHP execution and passes the rewritten query string. With Nginx, this includes the standard FastCGI parameters and PHP-FPM connection. With Apache, the PHP integration is an environment choice; the application routing file contains no PHP settings. See the complete application files in the next example.