Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use parse_url() to separate a complete URL into components, then pass its path to basename() for the last path segment. Append the query string separately if you need it. For the current incoming request, start with $_SERVER['REQUEST_URI'] instead of rebuilding the URL from the host and scheme.

Get the final filename and full query string from a complete URL

This matches the SitePoint forum question: extract shop-product.php and keep the parameters exactly as they appear, including percent-encoded values such as %20 and %27. Parse the URL, take the basename of its path, and append the query only when one exists:

<?php
$url = 'https://www.domain.com.au/dev/website/shop-product.php?shopCategoryID=1&shopSubCategoryID=2&productID=1&title=Buy%20Blank%20T-Shirts%20Online&category=Men%27s%20T-Shirts';

$parts = parse_url($url);

if ($parts === false) {
    throw new InvalidArgumentException('Malformed URL');
}

$path = $parts['path'] ?? '';
$filename = basename($path);
$query = $parts['query'] ?? null;
$result = $filename . ($query !== null ? '?' . $query : '');

echo $result;

The output is shop-product.php?shopCategoryID=1&shopSubCategoryID=2&productID=1&title=Buy%20Blank%20T-Shirts%20Online&category=Men%27s%20T-Shirts. parse_url() separates components; it does not URL-decode the returned values. That preserves the encoded representation when exact text matters. Decode only if you have a separate display or application requirement, and do not treat decoding as validation.

The PHP manual describes parse_url() as parsing a URL into the components that are present. It is a component parser, not a URL validator. If the input can be malformed, handle a failed parse and an empty path rather than assuming a filename is present.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Get the filename and extension without the query

Keep filename extraction separate from query handling. Give basename() only the parsed path so that neither query parameters nor a fragment can become part of the filename:

<?php
$path = parse_url($url, PHP_URL_PATH);
$filename = basename($path ?? '');
$extension = pathinfo($filename, PATHINFO_EXTENSION);

basename() returns the trailing name component of a path; see the PHP manual. To get extension information, pathinfo() can return a basename, filename, or extension. PATHINFO_EXTENSION is the text after the final dot: for archive.tar.gz, it returns gz, not tar.gz. Handling compound extensions is an application-specific convention. See the PHP pathinfo() manual.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Read the current request URL in PHP

When the code is handling the active HTTP request, use $_SERVER['REQUEST_URI'] for the request target—the path plus query, for example /dev/website/shop-product.php?productID=1:

<?php
$requestTarget = $_SERVER['REQUEST_URI'] ?? '';

$parts = parse_url($requestTarget);
if ($parts === false) {
    throw new InvalidArgumentException('Malformed request target');
}

$filename = basename($parts['path'] ?? '');
$query = $parts['query'] ?? null;
$result = $filename . ($query !== null ? '?' . $query : '');

If you need the entire request target, use $requestTarget directly; no filename extraction is necessary. The PHP manual notes that $_SERVER values depend on the server environment. Treat them as request data, not trusted filesystem paths.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Cases that can change the result

  • No path or a trailing slash: A URL may have no path, or its final path component may be empty. In those cases, there may be no filename to return.
  • Query without a filename: A URL can include a query even when its path has no file-like segment. Appending the query does not establish that a filename exists.
  • Fragments: The portion after # is a fragment, separate from both path and query. It is not included in a normal HTTP request target sent to the server.
  • Routes instead of files: A URL path can identify a routed endpoint or controller without naming a real file or showing an extension. The URL alone does not tell you what file, if any, the server returns.
  • Untrusted extensions: A filename extension does not prove a file’s type or safety. For uploads, remote files, or filesystem operations, validate according to the operation you intend to perform.

Which PHP function should you use?

Need Use What it gives you
Final segment of a path basename($path) The trailing name component, without query or fragment if you pass only the path.
Extension or filename parts pathinfo($filename, PATHINFO_EXTENSION) The suffix after the final dot; compound-extension rules are application-specific.
Separate a complete URL into path and query parse_url($url) URL components, without decoding their values; this is not URL validation.
Current request target $_SERVER['REQUEST_URI'] The incoming path and query, subject to server-environment behavior.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.