Remove unwanted query parameters from normalized URLs

This commit is contained in:
Jonny Barnes 2018-01-12 12:20:36 +00:00
parent 12a6b8f6b3
commit 6ccd564d56
2 changed files with 34 additions and 16 deletions

View file

@ -126,20 +126,33 @@ if (! function_exists('normalize_url')) {
// Sort GET params alphabetically, not because the RFC requires it but because it's cool!
if (isset($url['query'])) {
if (preg_match('/&/', $url['query'])) {
$s = explode('&', $url['query']);
$url['query'] = '';
sort($s);
foreach ($s as $z) {
$url['query'] .= "{$z}&";
$queries = explode('&', $url['query']);
$url['query'] = '';
sort($queries);
foreach ($queries as $query) {
//lets drop query params we dont want
$key = stristr($query, '=', true);
if (queryKeyIsBanned($key) === false) {
$url['query'] .= "{$query}&";
}
$url['query'] = preg_replace('/&\Z/', '', $url['query']);
}
$newUrl .= "?{$url['query']}";
$url['query'] = preg_replace('/&\Z/', '', $url['query']);
if ($url['query'] !== '') {
$newUrl .= "?{$url['query']}";
}
}
return $newUrl;
}
function queryKeyIsBanned(string $key): bool
{
$bannedKeys = [
'ref_src',
];
return in_array($key, $bannedKeys);
}
}
// sourced from https://stackoverflow.com/a/9776726