Fix router to properly set $_SERVER variables for entry points and pretty permalinks#89
Fix router to properly set $_SERVER variables for entry points and pretty permalinks#89
Conversation
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
There was a problem hiding this comment.
Code Review
This pull request fixes the built-in server's router to correctly handle $_SERVER variables, enabling support for pretty permalinks and direct access to PHP entry points like wp-login.php. The changes in router.php are solid, correctly detecting PHP files and setting the necessary server variables to mimic direct file access or route to index.php. The addition of test cases is great. I've suggested an improvement to the pretty permalinks test to make it more robust by testing an actual pretty URL instead of a query parameter-based one.
| And I run `wp option update permalink_structure '/%postname%/'` | ||
|
|
||
| When I run `curl -sS http://localhost:8183/?p=1` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Hello world! | ||
| """ |
There was a problem hiding this comment.
The scenario is named "Pretty permalinks", but the test uses curl ... /?p=1, which is a plain permalink. This doesn't fully validate that pretty permalinks are working as intended with the new router logic for non-existent files.
To make this test more robust and accurately reflect its name, I suggest testing an actual pretty URL like /hello-world/. This will require flushing the rewrite rules after setting the permalink structure. Using curl -L is also recommended to follow any potential redirects.
And I run `wp option update permalink_structure '/%postname%/'`
And I run `wp rewrite flush`
When I run `curl -sSL http://localhost:8183/hello-world/`
Then STDOUT should contain:
"""
Hello world!
"""
Plan: Support pretty permalinks and multiple WordPress entry points
Problem
The current router.php doesn't properly set
$_SERVERvariables when routing requests to PHP files. This causes issues with:wp-login.phpand other entry pointsRoot Cause
When the router requires a PHP file (like
wp-login.phporindex.php), the$_SERVERvariables still reflect the router script, not the actual PHP file being executed. WordPress relies on these variables to determine the current URL and script.Solution Plan
$_SERVERvariable handling in router.phpSCRIPT_NAMEto the correct PHP file being executedPHP_SELFto matchSCRIPT_NAMESCRIPT_FILENAMEto the full path of the PHP filestrpos)Changes Made
1. router.php
Improved PHP file detection:
strpos($wpcli_server_path, '.php')withpathinfo($wpcli_server_path, PATHINFO_EXTENSION) === 'php'.php(e.g.,/my.php-files/image.jpg)Fixed $_SERVER variables for PHP entry points:
When routing to any PHP file (wp-login.php, wp-admin/index.php, etc.):
$_SERVER['SCRIPT_NAME']= the requested PHP file path (e.g.,/wp-login.php)$_SERVER['PHP_SELF']= same as SCRIPT_NAME$_SERVER['SCRIPT_FILENAME']= full filesystem path to the PHP fileFixed $_SERVER variables for pretty permalinks:
When routing non-existent URLs to index.php:
$_SERVER['SCRIPT_NAME']=/index.php$_SERVER['PHP_SELF']=/index.php$_SERVER['SCRIPT_FILENAME']= full path to index.php$_SERVER['REQUEST_URI']= unchanged, preserving the pretty URL2. features/server.feature
Test scenarios:
http://protocol and checks for "wp-login.php" in HTML outputwp option update permalink_structureto set permalink structure, then tests accessing default post with query parameterManual Testing Completed
✅ Direct PHP file access (test-entry.php) - $_SERVER variables correctly set
✅ Pretty permalink routing (/some-pretty-url/) - routed to index.php with correct variables
✅ Static file serving (test.txt) - served correctly by PHP's built-in server
✅ Edge case: directory with .php in name - handled correctly with pathinfo()
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.