要从查询字符串中删除项目以进行重定向,您可以使用以下方法:
function removeQueryStringParameter(url, parameter) {
const urlParts = url.split('?');
if (urlParts.length >= 2) {
const queryString = urlParts[1];
const queryStringParts = queryString.split('&');
const index = queryStringParts.findIndex(param => param.startsWith(parameter));
if (index !== -1) {
queryStringParts.splice(index, 1);
}
return urlParts[0] + (queryStringParts.length > 0 ? '?' + queryStringParts.join('&') : '');
}
return url;
}
const redirectUrl = removeQueryStringParameter(window.location.href, 'parameterToRemove');
window.location.href = redirectUrl;
function removeQueryStringParameter($url, $parameter) {
$parsedUrl = parse_url($url);
if (isset($parsedUrl['query'])) {
parse_str($parsedUrl['query'], $queryString);
unset($queryString[$parameter]);
$parsedUrl['query'] = http_build_query($queryString);
}
return http_build_url($parsedUrl);
}
$redirectUrl = removeQueryStringParameter($_SERVER['HTTP_REFERER'], 'parameterToRemove');
header('Location: ' . $redirectUrl);
from urllib.parse import urlparse, urlunparse, parse_qs, urlencode
def remove_query_string_parameter(url, parameter):
parsed_url = urlparse(url)
query_string = parse_qs(parsed_url.query)
query_string.pop(parameter, None)
new_query_string = urlencode(query_string, doseq=True)
return urlunparse((
parsed_url.scheme,
parsed_url.netloc,
parsed_url.path,
parsed_url.params,
new_query_string,
parsed_url.fragment
))
redirect_url = remove_query_string_parameter(request.referrer, 'parameterToRemove')
return redirect(redirect_url)
这些方法可以帮助您从查询字符串中删除项目以进行重定向。请注意,这些代码示例仅供参考,您可能需要根据您的实际需求进行调整。
领取专属 10元无门槛券
手把手带您无忧上云