FIX: category slug route was not working for subfolder setup

FIX: category slug route was not working for subfolder setup

https://meta.discourse.org/t/relative-url-root-issues-incorrect-latest-link-incorrect-redirect/163266

URLs like /forum/c/staff (subfolder setup) were landing on /forum/forum/c/staff/3. Note the extra “/forum”. This commit strips the redundant subfolder path from category URL.

diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb
index 731feca..bd662ab 100644
--- a/app/controllers/list_controller.rb
+++ b/app/controllers/list_controller.rb
@@ -372,6 +372,10 @@ class ListController < ApplicationController
 
     if current_slug != real_slug
       url = request.fullpath.gsub(current_slug, real_slug)
+      if ActionController::Base.config.relative_url_root
+        url = url.sub(ActionController::Base.config.relative_url_root, "")
+      end
+
       return redirect_to path(url), status: 301
     end
 
diff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rb
index 3b54f45..1bcab45 100644
--- a/spec/requests/list_controller_spec.rb
+++ b/spec/requests/list_controller_spec.rb
@@ -729,7 +729,15 @@ RSpec.describe ListController do
     end
 
     context "with subfolder" do
-      it "redirects to URL containing the updated slug" do
+      it "main category redirects to URL containing the updated slug" do
+        set_subfolder "/forum"
+        get "/c/#{category.slug}"
+
+        expect(response.status).to eq(301)
+        expect(response).to redirect_to("/forum/c/#{category.slug}/#{category.id}")
+      end
+
+      it "sub-sub-category redirects to URL containing the updated slug" do
         set_subfolder "/forum"
         get "/c/hello/world/bye/#{subsubcategory.id}"
 

GitHub sha: 32d6286b

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/relative-url-root-issues-incorrect-latest-link-incorrect-redirect/163266/7