Diagnosing Empty $_SERVER Variables on IIS
When migrating a WordPress installation from an Apache environment to Windows/IIS, you might encounter a frustrating discrepancy where standard PHP variables like $_SERVER["REQUEST_URI"] return an empty string while $_SERVER["PHP_SELF"] only yields index.php. This behavior is not a bug in your PHP logic but a direct consequence of how the IIS application pool configuration interacts with fastcgi_fcgi.
In the scenario described, URLs follow the format http://www.example.com/OLD_FOLDER/index.php/post-title/. The goal is to extract the path segment /post-title/ to construct proper 301 redirects. On typical Linux setups using mod_php or FPM, $_SERVER["REQUEST_URI"] captures the full request path including query strings. However, on IIS, if the application pool settings are not explicitly configured for FastCgi, the server may strip certain headers before passing them to PHP.
The most common culprit is the "EnableCGI" or specific URL rewriting configurations within the IIS Manager. If the applicationHost.config file does not have the correct <rewrite> rules applied, or if the handler for .php files isn't set to a fast-cgi module that preserves the full URI, PHP receives an incomplete dataset. Specifically, on Windows/IIS, if you are running through IIS without configuring the fastcgi module correctly, $_SERVER["REQUEST_URI"] can indeed be empty because the server fails to pass the raw request line to the PHP interpreter in the expected format.
To fix this, you must ensure that your IIS site is configured to handle PHP requests via a FastCGI handler that preserves the full URI. In many default Windows installations, the fastcgi module might be enabled but not configured with the correct arguments to pass REQUEST_URI. You can verify this by checking the Application Pool settings in IIS Manager.
If you are relying on .htaccess style logic (which is common for WordPress), note that Apache handles .htaccess files differently than IIS. On IIS, you must use the URL Rewrite module to manage redirects rather than relying on index.php logic alone unless your application architecture explicitly supports it.
Here is a practical approach using PHP to robustly extract the path when $_SERVER["REQUEST_URI"] might be unreliable on specific IIS configurations:
<?php
// Attempt to get the full request URI from standard variables
$requestUri = $_SERVER['REQUEST_URI'] ?? null;
// Fallback if REQUEST_URI is empty (common on misconfigured IIS)
if (empty($requestUri)) {
// Try combining PHP_SELF and QUERY_STRING manually
$phpSelf = $_SERVER['PHP_SELF']; // Returns 'index.php'
$queryString = $_SERVER['QUERY_STRING'] ?? '';
if (!empty($phpSelf) && !empty($queryString)) {
// Construct the path assuming index.php is the entry point
// Remove the script name to get the relative path
$pathInfo = substr($phpSelf, 0, -strlen($phpSelf));
// This logic often fails on IIS if REQUEST_URI is truly empty
// A more reliable method on IIS involves checking SCRIPT_NAME
}
}
// The most reliable variable for path info on IIS when rewriting is active
$path = $_SERVER['SCRIPT_NAME'] ?? '';
$query = $_SERVER['QUERY_STRING'] ?? '';
// Reconstruct the full URL part without the query string
$currentPath = rtrim($path, '/');
However, the cleanest solution involves ensuring your IIS URL Rewrite module is configured to treat the REQUEST_URI as a variable. If you cannot modify the server configuration immediately, consider checking the APPLICATION_POOL settings. Ensure that "EnableCGI" is set appropriately or that you are using the standard PHP FastCGI Application Pool identity.
Another critical consideration is the presence of the mod_rewrite equivalent in IIS. If you are trying to replicate Apache behavior where .htaccess files define the routing, IIS does not process .htaccess in the same way. You must use the URL Rewrite module within IIS Manager to create the 301 redirects directly in the server configuration rather than expecting PHP to parse a non-existent .htaccess file.
If you are using Laravel or another modern framework that handles routing internally, ensure your APP_URL environment variable is set correctly in the .env file, as this often supersedes server-side variables for generating URLs. For legacy systems running raw PHP with WordPress, verifying that the fastcgi module arguments include FCGI_INHERIT=1 can sometimes restore the missing REQUEST_URI.
Ultimately, if the IIS configuration prevents access to the full URI via standard variables, the most robust path forward is to configure a 301 redirect rule directly in the IIS URL Rewrite module. This bypasses the need for PHP to calculate the redirect target based on server variables that are being stripped by the Windows handler stack.
