Why $_SERVER['SCRIPT_NAME'] Lies After URL Rewriting
When you encounter discrepancies between the browser's address bar and what your PHP script reports via $_SERVER variables, the culprit is almost always Apache's rewrite engine or a misconfiguration in how the request is processed. In the scenario where a direct request to http://sub.mydomain.com/file.php works perfectly but rewriting .php to /file causes $_SERVER['SCRIPT_NAME'] to return an incorrect path like /sub/file.php, you are facing a classic collision between physical file paths and logical URL rewrites.
The core issue lies in how $_SERVER['SCRIPT_NAME'] is populated. This variable reflects the path of the script that Apache actually executes, not necessarily the path the user requested. When you access /file directly on the server without rewriting, the request maps to a physical file (perhaps located at /var/www/sub/file.php). Consequently, $_SERVER['SCRIPT_NAME'] correctly identifies the executing script as /file.php.
However, once you introduce an .htaccess rule to strip the extension or map clean URLs, the request flow changes. Apache receives the request for /file, processes your rewrite rules, and internally rewrites it to a physical pathâlet's assume the actual file resides in a subdirectory structure like /var/www/sub/file.php. If your RewriteRule or Redirect logic isn't configured with the [L] (last) flag or if the physical location of your files is nested within a subdomain directory on the filesystem, SCRIPT_NAME will reflect that internal physical path.
In your specific case, where the output becomes http://sub.mydomain.com/sub/file.php, it indicates that the physical file resides in a directory named sub, but the rewrite rule intended to serve it at the root of that subdomain context failed to update the SCRIPT_NAME variable correctly before sending the response. The browser sees the rewritten URL, but PHP is reporting its internal filesystem path.
To resolve this reliably, you must stop relying solely on $_SERVER['SCRIPT_NAME'] when rewrites are active. Instead, construct the canonical URL using a combination of $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'], and a base path that accounts for your rewrite rules.
A more robust approach involves checking if the request URI matches the expected pattern and then appending the correct base path manually. Here is a pattern often used to handle this:
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'];
$path = $_SERVER['REQUEST_URI'];
// If you need the path without query strings
if (strpos($path, '?') !== false) {
$path = substr($path, 0, strpos($path, '?'));
}
echo $protocol . '://' . $host . $path;
This method uses REQUEST_URI, which preserves the original path requested by the client (e.g., /file) regardless of where the script physically lives. This is often the preferred method for generating canonical links or determining the current URL in a rewrite-enabled environment.
If you are working within a framework like Laravel, the framework abstracts these $_SERVER variables entirely, providing access to the url() helper or the request() facade, which automatically resolves the correct path based on your application's routing configuration. If you are maintaining legacy PHP code without a framework, understanding that SCRIPT_NAME is an implementation detail of the server execution environment rather than the user-visible URL is critical for building maintainable applications.