Get the Current URL in PHP: Request Path, Query, and Public Origin
In PHP, $_SERVER['REQUEST_URI'] usually contains the requested path and query. Check SCRIPT_NAME for the script path and $_GET for decoded query parameters. For public absolute URLs, use a configured origin with the site’s scheme, host, and port.
Distinguish the request path, query, and script
The values below illustrate a direct request without URL rewriting or a proxy. The PHP $_SERVER manual lists server and environment variables. Availability and meaning can vary by PHP setup. In CLI mode there is no incoming HTTP request.
https://www.example.test:8443/tools/url-info.php?q=a%20b&n=2#result
| Field | Example | Meaning |
|---|---|---|
HTTP_HOST |
www.example.test:8443 |
Requested host and optional port |
REQUEST_URI |
/tools/url-info.php?q=a%20b&n=2 |
Request path and raw query |
QUERY_STRING |
q=a%20b&n=2 |
Raw query after the question mark |
SCRIPT_NAME |
/tools/url-info.php |
URL path of the executing script |
PHP_SELF |
/tools/url-info.php |
Script path, possibly with PATH_INFO |
The value a%20b becomes a b in $_GET['q']. Applying urldecode() again can change a value that PHP has already decoded. Separate the path and query before decoding the specific component you need.
Inspect a real request with local PHP
Save the code as url-info.php and start PHP’s local server from the same folder. It accepts ordinary paths starting with one slash and splits the request target at the first question mark. It returns JSON and reports parsed_q as null when q is missing or an array.
<?php
declare(strict_types=1);
const PUBLIC_ORIGIN = 'https://www.example.test';
header('Content-Type: application/json; charset=UTF-8');
header('Cache-Control: no-store');
$target = $_SERVER['REQUEST_URI'] ?? null;
if (!is_string($target)
|| !str_starts_with($target, '/')
|| str_starts_with($target, '//')
|| str_contains($target, '\\')
|| preg_match('/[\x00-\x20\x7F#]/', $target) === 1) {
http_response_code(400);
echo '{"error":"Expected an HTTP request path."}';
exit;
}
[$path, $query] = array_pad(explode('?', $target, 2), 2, null);
$scriptName = $_SERVER['SCRIPT_NAME'] ?? '';
$q = $_GET['q'] ?? null;
$https = strtolower((string) ($_SERVER['HTTPS'] ?? ''));
$usesDirectTls = in_array($https, ['on', '1'], true);
echo json_encode([
'host_header' => $_SERVER['HTTP_HOST'] ?? null,
'request_uri' => $target,
'path' => $path,
'raw_query' => $query,
'query_string' => $_SERVER['QUERY_STRING'] ?? null,
'parsed_q' => is_string($q) ? $q : null,
'script_name' => $scriptName,
'php_self' => $_SERVER['PHP_SELF'] ?? null,
'script_basename' => basename($scriptName),
'direct_scheme' => $usesDirectTls ? 'https' : 'http',
'public_url' => PUBLIC_ORIGIN . $target,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE);
php -S 127.0.0.1:8080
Request http://127.0.0.1:8080/url-info.php?q=a%20b&n=2. Expect /url-info.php for path, q=a%20b&n=2 for raw_query, and a b for parsed_q. On this development server, /url-info.php/extra?q%5B%5D=x keeps /extra in path, keeps /url-info.php as script_name, and returns null for parsed_q. Run the PHP development server locally and stop it with Ctrl+C.
Build a URL from a trusted public origin
Treat HTTP_HOST as request metadata. The example sets the public origin in PUBLIC_ORIGIN, so the reported public_url starts with https://www.example.test even for a local request. If the application serves multiple domains, select one from a configured set of permitted domains.
When putting the output in an HTML link, escape it for the attribute context with htmlspecialchars(). Use parse_url() to extract URL parts when needed and validate the result separately for its intended use. For password reset or canonical links, use the application-defined path and only the parameters needed.
Understand HTTPS, proxies, and fragments
The direct_scheme field describes a setup where the web server sets HTTPS to on or 1. If a proxy terminates HTTPS and sends HTTP to PHP, this value can be http even when the visitor connects over HTTPS. Configure trusted proxy addresses and header handling using the trusted proxy guide.
A fragment such as #result is handled by the browser. PHP receives only the part before the fragment. If the application needs the fragment on the server, use browser code to send the specific value explicitly. Query strings can contain tokens and personal information. Keep the diagnostic script local and limit shared logs to fields you need.




