From 2293fca01277f0995f11d38e35c1b9be56e3b4da Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 24 May 2016 16:15:57 -0400 Subject: [PATCH] FEATURE: after category name is changed, URLs with old category slug and tag filter will redirect to new category slug --- app/controllers/tags_controller.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 6b25d361de8..6b08a54f3b0 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -167,11 +167,11 @@ class TagsController < ::ApplicationController parent_category_id = nil if parent_slug_or_id.present? parent_category_id = Category.query_parent_category(parent_slug_or_id) - raise Discourse::NotFound if parent_category_id.blank? + redirect_or_not_found and return if parent_category_id.blank? end @filter_on_category = Category.query_category(slug_or_id, parent_category_id) - raise Discourse::NotFound if !@filter_on_category + redirect_or_not_found and return if !@filter_on_category guardian.ensure_can_see!(@filter_on_category) end @@ -197,4 +197,17 @@ class TagsController < ::ApplicationController options end + + def redirect_or_not_found + # automatic redirects for renamed categories + url = params[:parent_category] ? "c/#{params[:parent_category]}/#{params[:category]}" : "c/#{params[:category]}" + permalink = Permalink.find_by_url(url) + + if permalink.present? && permalink.category_id + redirect_to "#{Discourse::base_uri}/tags#{permalink.target_url}/#{params[:tag_id]}", status: :moved_permanently + else + # redirect to 404 + raise Discourse::NotFound + end + end end