Canonical URLs for local state
If your page uses query strings for local-only state (like UI preferences, filters, or pagination), you should add a canonical URL to your page. This tells SEO crawlers to ignore the query string and index the page without it.Next.js App Router
In the Next.js app router, this is done via the metadata object:app/products/page.tsx
Next.js Pages Router
For the pages router, use theHead component:
pages/products.tsx
Canonical URLs with meaningful query strings
If however the query string is defining what content the page is displaying (like YouTube’s watch URLs:https://www.youtube.com/watch?v=dQw4w9WgXcQ),
your canonical URL should contain the relevant query strings.
You can use your parsers to read the search params and serialize the canonical URL:
app/watch/page.tsx
When to use canonical URLs
Use canonical URLs when:- UI state: Filters, sorting, view modes, expanded sections
- Pagination: Page numbers that don’t change core content
- Client preferences: Theme, language (if not affecting content), layout options
- Temporary state: Modal open/close, selected tab
- Content identification: Unique resource IDs (like video IDs, article slugs)
- Search queries: User search terms that change results
- Deep linking: State that defines what the user should see
- Shareability: URLs meant to be bookmarked or shared
Multiple query parameters
When you have multiple query parameters, decide which ones are meaningful for SEO:app/products/page.tsx
robots.txt considerations
For query parameters that are purely for UI state, you can also userobots.txt
to tell search engines to ignore specific parameters:
public/robots.txt
Open Graph and Twitter Cards
When sharing links on social media, you may want to remove query parameters from the Open Graph URL:app/products/page.tsx
Google Search Console
Monitor how Google indexes your pages with different query parameters:- Check the URL Parameters section in Google Search Console
- Configure how Googlebot should treat specific parameters
- Use the URL Inspection tool to verify canonical tags are working
Best practices summary
- Always set a canonical URL when using query strings for UI state
- Include meaningful query params in canonical URLs for content identification
- Use loaders and serializers to keep canonical URL logic DRY
- Test your canonical tags using browser DevTools or SEO analysis tools
- Monitor in Search Console to ensure proper indexing
- Be consistent - use the same canonical URL strategy across your site