Draft an edge SEO redirect normalization: path case and UTM stripping before…
- User
Draft an edge SEO redirect normalization: path case and UTM stripping before canonicalization.
Assistant
Draft an edge SEO redirect normalization: path case and UTM stripping before canonicalization.
1. Analyze the User's Request: * Core Task: Draft an "edge SEO redirect normalization." * Specifics: This involves two distinct operations to be performed "before canonicalization": 1. Path Case Normalization: Ensuring URLs are lowercase (or follow a specific casing rule). 2. UTM Stripping: Removing UTM parameters (tracking codes) from the URL. * Context: "Edge SEO" implies doing this at the edge (CDN level like Cloudflare, Fastly, Akamai, or edge functions like Vercel Edge Middleware/Cloudflare Workers) rather than on the origin server.
Determine the Strategy:
utm_* parameters? -> Remove them.Drafting the Code (Mental Sandbox):
const url = new URL(request.url);const originalPath = url.pathname;const normalizedPath = originalPath.toLowerCase();let needsRedirect = false;if (originalPath !== normalizedPath) needsRedirect = true;url.pathname = normalizedPath;url.searchParams.utm_.needsRedirect = true.needsRedirect, return Response.redirect(url.toString(), 301).Refining the Output:
Final Polish:
Constructing the Final Response:
(Self-Correction during drafting): Should I worry about preserving the hash (#)?
URL object in JS keeps the hash client-side usually, but on the edge, the hash isn't sent to the server.request.url won't have it. If I redirect, the browser will retain the hash. That's correct behavior.What about other parameters?
What about ?utm_source=... vs ?UTM_SOURCE=...?
utm_ case-insensitive, or just strictly utm_. Best practice is strict utm_ but checking keys case-insensitively is safer. Let's stick to standard string matching for simplicity or toLowerCase the key for checking.Final Plan:
This logic performs two specific cleanup actions:
/About-Us and /about-us).utm_.export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
let shouldRedirect = false;
// 1. PATH NORMALIZATION
// Convert path to lowercase.
// We assume no case-sensitive folders exist (standard for most modern sites).
const originalPath = url.pathname;
const normalizedPath = originalPath.toLowerCase();
if (originalPath !== normalizedPath) {
url.pathname = normalizedPath;
shouldRedirect = true;
}
// 2. UTM STRIPPING
// Iterate over query parameters and remove keys starting with "utm_"
// We clone searchParams.keys() to avoid mutation issues during iteration
const params = url.searchParams;
const originalParamCount = Array.from(params.keys()).length;
for (const key of Array.from(params.keys())) {
if (key.startsWith('utm_')) {
params.delete(key);
}
}
// Check if params actually changed to avoid redirecting just because we iterated
// We compare the stringified query string length or content
if (url.searchParams.toString() !== request.url.split('?')[1] || '') {
// Note: Simple check is usually fine, but strictly checking keys is better.
// Here we simply check if we deleted anything.
if (params.keys().length !== originalParamCount) {
shouldRedirect = true;
}
}
// Reconstruct the "clean" URL
// url.toString() automatically reconstructs path + remaining query params
// EDGE CASE HANDLING:
// Ensure we don't create an infinite loop or redirect unnecessarily
// if the only change was removed params resulting in an empty query string
if (shouldRedirect) {
return Response.redirect(url.toString(), 301);
}
// 3. CONTINUE TO ORIGIN
// If no normalization needed, fetch the original request
return fetch(request);
},
};
example.com/Products and example.com/products do not get indexed as two separate pages with duplicate content.?utm_source=email) and a direct link pass "link juice" to the clean, canonical URL (/products) rather than splitting authority across variations.