Same rel="canonical" for view pages with and without exposed filters

Created on 28 November 2022, over 2 years ago
Updated 21 August 2023, almost 2 years ago

Problem/Motivation

Same rel="canonical" for view pages with and without exposed filters.

Steps to reproduce

I have a view Cities with not required exposed filter country.

Case 1:
I left this filter empty all cities in all countries are shown.
<link rel="canonical" href="https://xn----7sblvlgns.xn--p1ai/cities" />
All is OK.

Case 2:
I select country Norway and all cities in all Norway are shown.
But canonical is still same.
But this is completely different page.
Should be <link rel="canonical" href="https://xn----7sblvlgns.xn--p1ai/cities?country=no" />
As a result this page was deleted from search engine ranking.

Proposed resolution

Add selected (not all) exposed filter values to rel="canonical"

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

As a workaround:

/**
 * Implements hook_page_attachments_alter().
 */
function my_module_page_attachments_alter(array &$attachments) {
  $route_name = \Drupal::routeMatch()->getRouteName();
  if ($route_name == 'view.cities.page') {
    $request = \Drupal::request();
    if ($request->query->has('country')) {
      $country_id = $request->query->get('country');
      $url = Url::fromRoute($route_name, [],
        [
          'query' => ['country' => $country_id],
        ])->setAbsolute();
      $attachments['#attached']['html_head']['canonical_url'] = [
        [
          '#tag' => 'link',
          '#attributes' => [
            'rel' => 'canonical',
            'href' => $url->toString(),
          ],
        ],
        'canonical_url',
      ];
    }
  }
}
✨ Feature request
Status

Postponed: needs info

Version

9.5

Component
ViewsΒ  β†’

Last updated about 5 hours ago

Created by

πŸ‡·πŸ‡ΈSerbia super_romeo Belgrade

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Should this be reopened or closed out?

  • Status changed to Closed: outdated about 2 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Few years with no follow up, going to close out. But if still a desired feature that someone feels belongs in core feel free to re-open.

    Thanks!

  • πŸ‡ΊπŸ‡ΈUnited States xjm

    Speaking as a Views maintainer, I think the option described would overload the UI. I think this would be more appropriately implemented in contrib. Thanks!

Production build 0.71.5 2024