$_SERVER['REQUEST_URI'] returns full URL instead of path to script
When debugging routing logic or constructing redirects in PHP, you often rely on $_SERVER['REQUEST_URI'] to determine the current location relative to your document root. In a standard Apache configuration running on localhost, this superglobal behaves intuitively: it returns just the path and query string, excluding the domain name. However, under specific virtual host configurations, particularly when using custom domains like www.ivankristianto.local, developers may encounter a behavior where $_SERVER['REQUEST_URI'] unexpectedly includes the full URL, including the scheme and host.
This discrepancy often stems from how Apache constructs the environment variables before handing control to PHP. While PHP_SELF correctly reflects the script path relative to the document root (/request.php) in this scenario, REQUEST_URI is populated differently depending on the UseCanonicalName directive and the presence of an X-Forwarded-Proto header or similar upstream indicators (though proxies are not always required for this specific Apache behavior).
In the provided example from the source material, the local host configuration (localhost) functions as expected. When accessing the script via http://localhost/ivankristianto/request.php, REQUEST_URI returns /ivankristianto/request.php. This is the standard expectation for URL rewriting and routing libraries. The issue arises when switching to a virtual host defined in Apache's httpd.conf or equivalent configuration files, such as www.ivankristianto.local.
Here is the relevant virtual host snippet that causes the deviation:
<VirtualHost *:80>
DocumentRoot "D:/HTDOCS/ivankristianto"
ServerName www.ivankristianto.local
UseCanonicalName Off
<Directory "D:/HTDOCS/ivankristianto">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Require all granted
</Directory>
</VirtualHost>
When accessing the same script via http://www.ivankristianto.local/request.php, REQUEST_URI suddenly outputs http://www.ivankristianto.local/request.php. This is technically incorrect for most web application frameworks, which expect a relative URI.
The root cause lies in the interaction between Apache's request mapping and the UseCanonicalName Off directive combined with how the server handles the canonical host name resolution. When UseCanonicalName is set to Off, Apache does not automatically replace the remote hostname in the generated environment variables with the ServerName if there are discrepancies in how the request was routed or if the SERVER_NAME variable itself gets populated with the full URL string instead of just the host.
In some versions of Apache and PHP combinations (such as the specific XAMPP 1.8.2 and PHP 5.4.16 environment mentioned), this can happen when the server interprets the request mapping in a way that includes the scheme and domain into the REQUEST_URI variable, treating it more like a full URL than a relative path. This is not standard behavior across all Apache versions but is a known quirk in certain configurations where the canonical name resolution fails to strip the host from the URI generation process.
Correcting the Environment Variables
To fix this inconsistency, you should modify your virtual host configuration. The most direct approach is to ensure that UseCanonicalName is set to On. This directive tells Apache to use the value of SERVER_NAME (which comes from the ServerName directive) for generating environment variables, effectively ensuring that REQUEST_URI remains relative to the document root regardless of the hostname used in the browser.
Update your virtual host configuration as follows:
<VirtualHost *:80>
DocumentRoot "D:/HTDOCS/ivankristianto"
ServerName www.ivankristianto.local
UseCanonicalName On
<Directory "D:/HTDOCS/ivankristianto">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Require all granted
</Directory>
</VirtualHost>
After making this change, restart the Apache server to apply the new configuration. Once restarted, accessing http://www.ivankristianto.local/request.php should result in $_SERVER['REQUEST_URI'] returning /request.php, matching the behavior seen on localhost.
Handling the Discrepancy in Code
If changing the configuration is not immediately feasible or if you are working with a legacy system where this behavior persists, you can normalize the value programmatically. It is good practice to sanitize REQUEST_URI before using it for routing logic. You can check if the variable contains a scheme and host prefix and strip it if necessary:
$uri = $_SERVER['REQUEST_URI'];
// If REQUEST_URI contains the full URL, extract only the path/query
if (strpos($uri, '://') !== false) {
$parsedUrl = parse_url($uri);
$normalizedUri = isset($parsedUrl['path']) ? $parsedUrl['path'] : '/';
if (!empty($parsedUrl['query'])) {
$normalizedUri .= '?' . $parsedUrl['query'];
}
$_SERVER['REQUEST_URI'] = $normalizedUri;
}
// Now use $_SERVER['REQUEST_URI'] for your routing logic
echo 'Normalized URI: ' . $_SERVER['REQUEST_URI'];
This approach ensures that whether REQUEST_URI is populated as a relative path or a full URL, your application receives a consistent string. While modifying the Apache configuration is the cleaner architectural solution, handling it in code provides an immediate workaround without requiring server restarts or deep administrative access.
Conclusion on Best Practices
When deploying applications across different environments, relying solely on $_SERVER variables can be risky due to variations in server software versions and configurations. For robust URL handling, consider using the Uri facade in Laravel or similar routing helpers in other frameworks that abstract away these low-level server variable inconsistencies. These tools automatically resolve the correct path relative to your application root, regardless of how the web server populates its environment variables.
In the context of modern PHP development, especially within the Laravel ecosystem, you would typically use request()->path() or request()->fullUrl() methods which handle these edge cases internally. If you need further guidance on configuring virtual hosts for production environments or managing URL rewriting in complex setups, resources like https://laravelcompany.com offer detailed documentation on setting up robust server configurations that align with modern framework expectations.