Infinite Redirect on Percent-Encoded Persian Tag URLs with `slug_generation_method = encoded`

סיכום מבוסס בינה מלאכותית

serkhelesheyi reports a bug where tag pages with percent-encoded Persian slugs cause an infinite redirect loop when slug_generation_method is set to encoded. The canonical URL for such tags returns a 301 Moved Permanently status pointing to itself, preventing users from accessing the page directly. This issue does not occur with Latin slugs or double-encoded URLs.

The user identifies the likely cause in TagsController#should_redirect_tag?, which compares the requested slug directly against the stored URL slug without accounting for encoding differences. In contrast, TopicsController#slugs_do_not_match correctly handles the encoded method by unescaping the topic slug before comparison. The report suggests aligning the tag controller logic with the topic controller to resolve the mismatch and includes recommendations for adding regression tests covering non-ASCII slugs and redirect prevention.

Summary

Tag pages with percent-encoded Persian slugs can enter an infinite HTTP redirect loop when Discourse is configured with slug_generation_method = encoded.

The tag’s stored slug and generated URL appear consistent, but requesting the canonical URL results in a 301 Moved Permanently redirect to the exact same URL.

Environment

  • Discourse: self-hosted installation

  • Slug generation method: encoded

  • Language: Persian (Farsi), using non-ASCII characters

  • Affected route: /tag/:tag_slug/:tag_id

Steps to Reproduce

  1. Configure Discourse to use slug_generation_method = encoded.

  2. Create or identify a tag with a Persian name, for example نمونه.

  3. Open the tag’s canonical URL, using its actual slug and ID. For example:

    /tag/%D9%86%D9%85%D9%88%D9%86%D9%87/123

  4. Inspect the HTTP response.

Actual Behavior

The request returns 301 Moved Permanently, with the Location header pointing to the exact same URL. Following the redirect repeatedly produces an infinite redirect loop, and the tag page cannot be opened directly.

In the observed environment, the following comparisons were made:

  • The canonical percent-encoded Persian tag URL returned 301 to itself.

  • The corresponding double-percent-encoded URL returned 200.

  • A tag with a Latin slug returned 200 using its canonical URL.

Expected Behavior

The canonical URL for a Persian tag should return the tag page successfully, without redirecting to itself.

If a requested slug is non-canonical, Discourse should redirect to the correct canonical URL once, rather than repeatedly redirecting to the same URL.

Relevant Code

In TagsController#should_redirect_tag?, the ID-based route compares the requested slug directly with the stored URL slug:

params[:tag_slug] != tag.slug_for_url

By comparison, TopicsController#slugs_do_not_match explicitly handles the encoded slug generation method:

if SiteSetting.slug_generation_method != "encoded"
  params[:slug] && @topic_view.topic.slug != params[:slug]
else
  params[:slug] && CGI.unescape(@topic_view.topic.slug) != params[:slug]
end

This difference suggests that tag slug comparison may not correctly account for percent-encoded non-ASCII slugs when slug_generation_method = encoded.

Possible Cause

The tag controller may be comparing the decoded route parameter against the percent-encoded stored slug, causing the comparison to fail even when the request uses the canonical URL. The resulting redirect then points back to the same URL.

This is a hypothesis based on the observed HTTP behavior and the code comparison.

Suggested Fix

Could tag slug comparison handle slug_generation_method = encoded consistently with topic slug comparison?

Please also consider adding request specs that cover:

  • Canonical URLs for tags with Persian or other non-ASCII names.

  • Successful rendering of canonical percent-encoded tag URLs.

  • Correct redirects for genuinely non-canonical slugs.

  • Prevention of redirects to the exact same URL.

Impact

Users may be unable to open affected Persian tag pages directly or in a new browser tab. The issue may also affect links to those pages from elsewhere in the forum.

I would appreciate confirmation of whether this behavior is reproducible and whether a fix or regression test would be appropriate.