Skip to content

A platform for community discussion. Free, open, simple.

FIX: Numeric-tag fallback disables canonical redirect on slug-bearing routes (#39984)

discourse-patch-triage[bot]6bdab2b11d0f
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
index cf8f0554f7c..9164c890979 100644
--- a/app/controllers/tags_controller.rb
+++ b/app/controllers/tags_controller.rb
@@ -544,8 +544,8 @@ class TagsController < ::ApplicationController
return false if request.format.json?
# intersection routes use tag_name, not tag_slug/tag_id - don't redirect
return false if params[:additional_tag_names].present?
- # don't redirect if we found the tag by name (numeric tag name on legacy route)
- return false if @tag_found_by_name
+ # numeric-name fallback should only suppress redirects on legacy routes without a slug
+ return false if @tag_found_by_name && params[:tag_slug].blank?
if params[:tag_id].present?
# new format - redirect if slug doesn't match
diff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rb
index cb712f59f09..7e7e8b460c8 100644
--- a/spec/requests/tags_controller_spec.rb
+++ b/spec/requests/tags_controller_spec.rb
@@ -569,6 +569,19 @@ RSpec.describe TagsController do
expect(response.body).not_to include("ActionView::Template::Error")
end
+ it "redirects slug routes for numeric tag names to the canonical slug/id URL" do
+ numeric_tag_name = (Tag.maximum(:id).to_i + 10_000).to_s
+ numeric_tag = Fabricate(:tag, name: numeric_tag_name)
+ Fabricate(:topic, tags: [numeric_tag])
+
+ get "/tag/not-the-slug/#{numeric_tag_name}"
+
+ expect(response.status).to eq(301)
+ expect(response.redirect_url).to end_with(
+ "/tag/#{numeric_tag.slug_for_url}/#{numeric_tag.id}",
+ )
+ end
+
context "with a category in the path" do
fab!(:topic_in_category) { Fabricate(:topic, tags: [tag], category: category) }