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

To remove one query parameter after a PHP GET form submits while keeping the others, parse the query string, unset the target key, rebuild the URL, then send a redirect and stop execution. Validate and authorize the submitted values before processing them; cleaning the URL does not replace those checks.

Remove one query parameter and keep the rest

Place this after the application has read and validated the submitted values and completed any intended server-side action. Replace remove_me with the exact query key to discard.

<?php
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
parse_str($_SERVER['QUERY_STRING'] ?? '', $query);
unset($query['remove_me']);
$location = $path . ($query ? '?' . http_build_query($query, '', '&', PHP_QUERY_RFC3986) : '');
header('Location: ' . $location, true, 303);
exit;

For example, if the request is /results.php?remove_me=sent&category=books&page=2, the redirect goes to /results.php?category=books&page=2. If the removed key was the only parameter, the destination is just /results.php.

parse_str() parses the query string into the supplied array. Its result-array argument is mandatory from PHP 8.0 onward, and the older form that creates variables implicitly is discouraged (PHP manual: parse_str()). http_build_query() turns the remaining array back into a URL-encoded query string (PHP manual: http_build_query()).

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

Why the redirect is a 303

A GET submission is already a GET request, but a 303 See Other response tells the browser to make a follow-up GET request to the cleaned location. The header() call must happen before any page output, and exit prevents the request from continuing to render or perform further work.

Keep the redirect destination local

The example derives the destination path from the current request rather than accepting an arbitrary URL from submitted data. parse_url() extracts URL components; it does not validate a URL, and its returned parts are not URL-decoded (PHP manual: parse_url()). Do not put an untrusted, user-supplied absolute URL directly into the Location header.

Choose which query values survive

Unset one key to preserve the current state

The example removes only remove_me. Other values, such as filters and page numbers, remain in the rebuilt query. This suits a one-time flag or parameter when the rest of the current URL should be retained.

Rebuild an allow-list for tighter control

If only a known set of parameters should persist, construct a new array from those permitted keys instead of copying the entire parsed query and unsetting one item. This makes the redirect’s retained state explicit. In either approach, validate values before using them in application logic.

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

Choose query-string encoding

The code passes PHP_QUERY_RFC3986 to http_build_query(). PHP’s default RFC 1738 mode encodes spaces as plus signs; RFC 3986 mode percent-encodes them. Use the mode that matches the application’s interoperability requirements (PHP manual: http_build_query()).

When a client-side URL update is preferable

JavaScript’s history.replaceState() can change the address bar without a server redirect round trip. It requires JavaScript, however, and does not undo server-side processing that already occurred. Use the PHP redirect when the server should issue the cleaned URL as the next request; use client-side history manipulation only when an address-bar update in the current page is sufficient.

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

Fragments are not included in the request

A URL fragment, such as #section, is not sent to the server in an HTTP request. The PHP code therefore cannot recover and preserve an incoming fragment from $_SERVER['REQUEST_URI']. If the fragment must remain, handle it in client-side code or add it deliberately from trusted data.

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.

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.