UTM Cleaner + Canonical Redirect Helper (PHP)

December 12, 2025

UTM / Tracking Param Cleaner (301 Canonical Redirect) #php #seo #redirects #analytics A tiny drop-in helper that strips tracking params like utm_*, gclid, fbclid and 301-redirects to a clean canonical URL. Useful to reduce duplicate indexing + keep your analytics cleaner. Install: 1) Put this in /utm-clean.php 2) Include it at the very top of pages you want cleaned (before output) ```php
<?php
// utm-clean.php — drop-in querystring cleaner
// Remove common tracking params and redirect to a clean URL (301).
declare(strict_types=1);

$kill = [
  'utm_source','utm_medium','utm_campaign','utm_term','utm_content',
  'gclid','fbclid','msclkid','igshid'
];

$qs = $_GET;
$changed = false;

foreach ($kill as $k) {
  if (array_key_exists($k, $qs)) {
    unset($qs[$k]);
    $changed = true;
  }
}

// Only redirect on GET requests
if ($changed && (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'GET')) {
  $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
  $host   = $_SERVER['HTTP_HOST'] ?? 'localhost';
  $path   = strtok($_SERVER['REQUEST_URI'] ?? '/', '?');

  $newQs = http_build_query($qs);
  $dest  = $scheme . '://' . $host . $path . ($newQs ? ('?' . $newQs) : '');

  header('Location: ' . $dest, true, 301);
  exit;
}
?>

Comments (0)

No comments yet — be the first.

← Back to all scripts