PHP_SELF vs PATH_INFO vs SCRIPT_NAME vs REQUEST_URI

PHP_SELF vs PATH_INFO vs SCRIPT_NAME vs REQUEST_URI

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding PHP URI Superglobals: A Practical Guide for Framework Developers

When building a PHP application behind a framework like CodeIgniter, handling URL routing often leads to confusion regarding which server variable holds the "true" address of the resource. Consider a setup where mod_rewrite is configured to strip index.php from the public-facing URI while forwarding everything to that single entry point. The resulting flow looks like this:

RewriteEngine on
RewriteCond $1 !^(images|inc|favicon\.ico|index\.php|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L] 

A request to http://www.example.com/faq/whatever physically hits index.php, yet the application logic needs to know it is handling /faq/whatever. Relying on $_SERVER['PHP_SELF'] in this scenario is a common mistake; because Apache passes the literal script name, PHP_SELF will always return index.php, offering no insight into the requested resource path.

To build reliable navigation and routing logic without fragile workarounds, you must understand the distinct roles of $_SERVER['SCRIPT_NAME'], $_SERVER['PATH_INFO'], and $_SERVER['REQUEST_URI']. Each serves a specific purpose in the HTTP request lifecycle, but they behave differently when rewriting is involved.

The Role of SCRIPT_NAME

SCRIPT_NAME indicates the physical path to the file executing the script relative to the document root. In the CodeIgniter example above, this variable will consistently return index.php. While useful for determining which entry file is active, it fails to capture the dynamic route information (like /faq/whatever) because that data is appended as a query string or passed via internal framework routing mechanisms rather than changing the physical script location.

Understanding PATH_INFO

PATH_INFO represents the portion of the URI path that follows the script name but before the query string. In a non-rewritten scenario, if you accessed example.com/index.php/faq, PATH_INFO would hold /faq. However, in the rewritten CodeIgniter setup, Apache strips everything after the file extension from the physical request path before invoking the handler. Consequently, PATH_INFO often becomes empty or unreliable because the web server has already resolved the rewrite rule and passed the entire path as a query parameter to the script (e.g., index.php?/faq/whatever).

The Reliability of REQUEST_URI

For most modern PHP applications using frameworks like CodeIgniter or Laravel, $_SERVER['REQUEST_URI'] is the most robust source for reconstructing the original request address. Unlike PATH_INFO, which is subject to server-specific parsing behaviors and can be wiped clean by rewrite rules, REQUEST_URI contains the raw URI requested by the client.

In the rewritten example, REQUEST_URI will retain the value /faq/whatever (or sometimes the full path including the domain depending on configuration). This makes it ideal for generating canonical URLs, logging access patterns, or determining which navigation links to display. If you need to reconstruct the absolute URL, you typically combine $_SERVER['REQUEST_URI'] with $_SERVER['SCRIPT_NAME'] if the entry file is not at the root, though many frameworks handle this internally via a base URL helper.

Best Practices for Framework Integration

When integrating these variables into your navigation logic, avoid checking PHP_SELF to determine the current route. Instead, rely on the framework's own router to provide the current segment if available. If you must bypass the framework layer—for instance, in a legacy script or a custom bootstrap file—using $_SERVER['REQUEST_URI'] allows you to parse the path without worrying about whether mod_rewrite has altered the physical execution path.

The choice between these variables depends entirely on your architecture. If you are using a framework with its own routing engine, trust the router's output. If you are writing raw PHP scripts where mod_rewrite is active, REQUEST_URI provides the necessary fidelity to reconstruct the user's intended destination without needing to guess what the server has internally transformed the request into.

Final Recommendation

In environments utilizing index.php as an entry point with rewrite rules, treat SCRIPT_NAME as a constant identifier for your entry file and use REQUEST_URI for all logic requiring knowledge of the requested resource. This approach minimizes the risk of broken links or incorrect navigation states caused by inconsistent server variable behavior across different hosting environments.

Our network