Get the path with query string on the current http request in PHP

Get the path with query string on the current http request in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Retrieving the Full Path, Including Query Strings, in PHP

In many web applications, reconstructing the full request path—including the query string—is necessary for logging, analytics, or redirect logic. A common requirement is to extract a URL like http://www.example.com/example/test/hi.php?randomvariable=1 and convert it into /example/test/hi.php?randomvariable=1. This transformation strips the scheme (protocol) and host while preserving the path component and its appended parameters.

The most straightforward approach in PHP relies on the $_SERVER['REQUEST_URI'] superglobal. This variable contains exactly what you need: the URI portion of the request as interpreted by the server, excluding the host and scheme. For the example URL provided, $_SERVER['REQUEST_URI'] will return /example/test/hi.php?randomvariable=1.

Using this superglobal is generally preferred over manually parsing $_SERVER['PATH_INFO'] or concatenating $_SERVER['SCRIPT_NAME'] with $_GET. The manual combination of script name and path info often fails to include query strings, as the $_GET array only contains parsed parameters, not their original string representation. By relying on REQUEST_URI, you avoid these edge cases where the framework or server might alter how these components are exposed.

Handling Protocol Relative URLs

While REQUEST_URI provides the raw path and query string, some applications require the path to be protocol-relative (starting with a slash but without http://). The superglobal already satisfies this condition by default, making it immediately usable for generating relative links or logging entries that need to remain independent of the current protocol context.

However, there are scenarios where you might want to normalize the URL further. For instance, if your application runs behind a reverse proxy and the REQUEST_URI does not match the actual external-facing URL due to header rewriting issues, you may need to inspect HTTP_X_ORIGINAL_URI or similar headers depending on your server configuration. Always verify that REQUEST_URI reflects the true request intent, especially in environments using load balancers like Nginx or Apache with proxy settings.

Framework-Specific Considerations

If you are working within a framework like Laravel, the approach remains consistent but benefits from built-in abstractions. In Laravel, you can access the same information via the request() helper:

$path = request()->fullUrl(); // Includes scheme and host
$relativePath = request()->path() . '?' . http_build_query(request()->query());

Note that request()->path() does not include the query string, so you must manually reconstruct it. This is why using REQUEST_URI directly at the framework or native level remains a robust choice for this specific task. LaravelCompany offers resources on building secure and efficient applications in PHP, which often emphasize leveraging built-in server variables when possible to avoid unnecessary abstraction overhead unless a framework-specific feature is required.

Potential Pitfalls and Edge Cases

One critical pitfall arises when the query string contains special characters or spaces. Using http_build_query() or manually joining $_GET keys may alter the original formatting of the query string, potentially breaking URLs that rely on specific encoding or ordering. Always prefer using REQUEST_URI to preserve the exact format sent by the client unless you have a strict need to parse and re-encode components.

Another consideration is how different web servers handle trailing slashes. Some configurations may append or omit them inconsistently, which could affect routing logic if your application relies on strict path matching. Be cautious when using this value for conditional logic without normalizing it first.

In summary, retrieving the path with query string in PHP is best achieved through $_SERVER['REQUEST_URI']. It is reliable, framework-agnostic, and preserves the exact structure of the incoming request.

Our network