From 94535d753a74e38b618f8b3b71e2c0ddb75ec846 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 2 Nov 2026 15:41:29 +0000 Subject: [PATCH 1/2] Respond with a 404 when there is no index.php to route to Co-Authored-By: Claude Opus 5.5 --- features/server.feature | 20 ++++++++++++++++++++ router.php | 3 +++ 2 files changed, 23 insertions(+) diff --git a/features/server.feature b/features/server.feature index 6c71ec8..b428864 100644 --- a/features/server.feature +++ b/features/server.feature @@ -132,3 +132,23 @@ Feature: Serve WordPress locally """ 1338 """ + + Scenario: Respond with a 404 for a missing file in a directory without an index.php + Given an empty directory + And a static.txt file: + """ + Static content + """ + And I launch in the background `wp server --host=localhost --port=8188 --docroot=.` + + When I run `curl -sS http://localhost:8188/static.txt` + Then STDOUT should contain: + """ + Static content + """ + + When I run `curl -sS -o /dev/null -w '%{http_code}' http://localhost:8188/missing.zip` + Then STDOUT should be: + """ + 404 + """ diff --git a/router.php b/router.php index 2db7738..9284da1 100644 --- a/router.php +++ b/router.php @@ -153,6 +153,9 @@ static function ( $buffer ) { } else { return false; } +} elseif ( ! is_file( $wpcli_server_root . DIRECTORY_SEPARATOR . 'index.php' ) ) { + // Without an index.php to route to, let the built-in web server respond with a 404. + return false; } else { // File doesn't exist or is outside document root - route to index.php for pretty permalinks $_SERVER['SCRIPT_NAME'] = '/index.php'; From 5c3dcf157d8894d6bcb7630c40d3692e78f289f0 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 2 Nov 2026 15:51:05 +0000 Subject: [PATCH 2/2] Use a port of its own for the 404 scenario Co-Authored-By: Claude Opus 5.5 --- features/server.feature | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/features/server.feature b/features/server.feature index b428864..57368a6 100644 --- a/features/server.feature +++ b/features/server.feature @@ -139,15 +139,15 @@ Feature: Serve WordPress locally """ Static content """ - And I launch in the background `wp server --host=localhost --port=8188 --docroot=.` + And I launch in the background `wp server --host=localhost --port=8189 --docroot=.` - When I run `curl -sS http://localhost:8188/static.txt` + When I run `curl -sS http://localhost:8189/static.txt` Then STDOUT should contain: """ Static content """ - When I run `curl -sS -o /dev/null -w '%{http_code}' http://localhost:8188/missing.zip` + When I run `curl -sS -o /dev/null -w '%{http_code}' http://localhost:8189/missing.zip` Then STDOUT should be: """ 404