Skip to content

Category::getUrl() should not fetch 301 redirect #38152

@kweij

Description

@kweij

Preconditions and environment

  • Magento version: 2.4.6-p3

Steps to reproduce

  1. Create a category in the Admin
    URL Key is now auto-generated, system/auto-generated URL Rewrite is created (redirect_type = 0)
  2. Change category URL Key
    System/auto-generated URL Rewrite is updated to a 301 from old to new (redirect_type = 301)
    A new system/auto-generated URL Rewrite is added (redirect_type = 0) from new tot internal route
  3. Enable canonical URLs
    bin/magento config:set catalog/seo/category_canonical_tag 1
  4. View the category in the frontend

State in the database with example data:

image

Expected result

Canonical URL === New category URL Key

Actual result

Canonical URL === Old category URL Key

Additional information

In \Magento\Catalog\Model\Category.php::getUrl() URL rewrites are loaded for the category by ID and store. This returns the first result, which is the 301 redirect from the old to the new category URL. The request_path of this rewrite is used to form the return value of Category.php:getUrl(), which contains the old URL.

The fix is that only the system/auto-generated URL rewrite is loaded. Therefor the array param provided to \Magento\UrlRewrite\Model\UrlFinderInterface::findOneByData() in vendor/magento/module-catalog/Model/Category.php:613 should be extended with UrlRewrite::REDIRECT_TYPE => 0,. The same addition is already in place for \Magento\Catalog\Model\Product\Url::getProductUrl(), but missing for categories.

Release note

Note that I've only yet encountered this issue in the category canonicals, but since the issue resides in Category.php:getUrl(), there are probably other area's where this bug causes an issue.

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: SEOComponent: UrlIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P2A defect with this priority could have functionality issues which are not to expectations.Progress: doneReported on 2.4.6-p3Indicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branch

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions