A platform for community discussion. Free, open, simple.
FIX: Redirect outdated encoded category slugs instead of returning 404 (#45332)
Arpit Jalan26d8f400902a
diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rbindex cb84cd17f07..6f2908e6216 100644--- a/app/controllers/list_controller.rb+++ b/app/controllers/list_controller.rb@@ -469,15 +469,10 @@ class ListController < ApplicationControllerendreal_slug = @category.full_slug("/")if CGI.unescape(current_slug) != CGI.unescape(real_slug)- path = CGI.unescape(request.path)- query = request.query_string- new_path = path.gsub(current_slug, real_slug)- url = query.present? ? "#{new_path}?#{query}" : new_path- if ActionController::Base.config.relative_url_root- url = url.sub(ActionController::Base.config.relative_url_root, "")- end-- return redirect_to path(url), status: :moved_permanently+ return(+ redirect_to category_redirect_url(category_slug_path_with_id, real_slug),+ status: :moved_permanently+ )end@description_meta =@@ -492,6 +487,23 @@ class ListController < ApplicationControllerendend+ # The canonical URL of the category, keeping whatever followed the slug path+ # in the request (such as "/l/latest" or ".json") and the query string.+ #+ # `canonical_slug_path` comes from `Category#url` and is already encoded. It+ # is used rather than substituting into the request path: with encoded slugs+ # the decoded path contains raw non-ASCII characters, which Rails refuses to+ # redirect to.+ def category_redirect_url(requested_slug_path, canonical_slug_path)+ requested_prefix = "/c/#{requested_slug_path}"+ path_info = UrlHelper.unencode(request.path_info)+ rest = path_info.start_with?(requested_prefix) ? path_info.delete_prefix(requested_prefix) : ""++ url = "#{Discourse.base_path}/c/#{canonical_slug_path}#{rest}"+ url = "#{url}?#{request.query_string}" if request.query_string.present?+ url+ end+def generate_list_for(action, target_user, opts)TopicQuery.new(current_user, opts).public_send("list_#{action}", target_user)enddiff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rbindex 50eb4397233..9792aa70272 100644--- a/spec/requests/list_controller_spec.rb+++ b/spec/requests/list_controller_spec.rb@@ -1833,6 +1833,80 @@ RSpec.describe ListController doendend+ context "with encoded non-ASCII slugs" do+ before { SiteSetting.slug_generation_method = "encoded" }++ {+ "zh_CN" => %w[产品 问题],+ "zh_TW" => %w[產品 問題],+ "ja" => %w[製品 質問],+ }.each do |locale, (old_name, new_name)|+ context "with a #{locale} category" do+ let(:category) { Fabricate(:category, name: old_name) }+ let!(:old_slug) { category.slug }++ before { category.update!(slug: new_name) }++ it "redirects an outdated slug to the current encoded slug" do+ get "/c/#{old_slug}/#{category.id}"++ expect(response.status).to eq(301)+ expect(response).to redirect_to("/c/#{CGI.escape(new_name)}/#{category.id}")++ get response.location+ expect(response.status).to eq(200)+ end++ it "redirects the slug without an id to the URL with the id" do+ get "/c/#{category.slug}"++ expect(response.status).to eq(301)+ expect(response).to redirect_to("/c/#{category.slug}/#{category.id}")+ end++ it "keeps the filter and the query string" do+ get "/c/#{old_slug}/#{category.id}/l/latest?page=2"++ expect(response.status).to eq(301)+ expect(response).to redirect_to("/c/#{category.slug}/#{category.id}/l/latest?page=2")+ end++ it "keeps the format" do+ get "/c/#{old_slug}/#{category.id}.json"++ expect(response.status).to eq(301)+ expect(response).to redirect_to("/c/#{category.slug}/#{category.id}.json")+ end+ end+ end++ it "redirects an outdated parent slug for a subcategory" do+ parent = Fabricate(:category, name: "製品")+ old_parent_slug = parent.slug+ subcategory = Fabricate(:category, name: "質問", parent_category: parent)+ parent.update!(slug: "新製品")++ get "/c/#{old_parent_slug}/#{subcategory.slug}/#{subcategory.id}/none"++ expect(response.status).to eq(301)+ expect(response).to redirect_to(+ "/c/#{parent.slug}/#{subcategory.slug}/#{subcategory.id}/none",+ )+ end++ it "keeps the subfolder" do+ set_subfolder "/forum"+ category = Fabricate(:category, name: "製品")+ old_slug = category.slug+ category.update!(slug: "質問")++ get "/c/#{old_slug}/#{category.id}/l/latest"++ expect(response.status).to eq(301)+ expect(response).to redirect_to("/forum/c/#{category.slug}/#{category.id}/l/latest")+ end+ end+context "with subfolder" doit "main category redirects to URL containing the updated slug" doset_subfolder "/forum"