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
-
Configure Discourse to use
slug_generation_method = encoded. -
Create or identify a tag with a Persian name, for example
نمونه. -
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 -
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
301to itself. -
The corresponding double-percent-encoded URL returned
200. -
A tag with a Latin slug returned
200using 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.