document.addEventListener('DOMContentLoaded', function() { const url = new URL(window.location.href); const pageParam = url.searchParams.get('_page'); // Or other page parameter if (pageParam && pageParam > 1) { // Get the base URL (e.g., yourdomain.com) const baseUrl = url.origin + url.pathname; // Remove any existing page parameter from the base URL const canonicalUrl = baseUrl.split('?')[0]; // Create or update the canonical link tag let canonicalLink = document.querySelector('link[rel="canonical"]'); if (!canonicalLink) { canonicalLink = document.createElement('link'); canonicalLink.rel = 'canonical'; document.head.appendChild(canonicalLink); } canonicalLink.href = canonicalUrl; } });