diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d5adf6ee1a0..79371c9423a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -224,13 +224,8 @@ class ApplicationController < ActionController::Base if permalink.present? # permalink present, redirect to that URL - if permalink.external_url - redirect_with_client_support permalink.external_url, status: :moved_permanently - return - elsif permalink.target_url - redirect_with_client_support "#{Discourse::base_uri}#{permalink.target_url}", status: :moved_permanently - return - end + redirect_with_client_support permalink.target_url, status: :moved_permanently + return end end diff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rb index 5ad3aa60993..957607236d5 100644 --- a/spec/requests/application_controller_spec.rb +++ b/spec/requests/application_controller_spec.rb @@ -37,6 +37,34 @@ RSpec.describe ApplicationController do expect(response.body).to eq(external_url) end + it 'supports subfolder with permalinks' do + GlobalSetting.stubs(:relative_url_root).returns('/forum') + Discourse.stubs(:base_uri).returns("/forum") + + trashed_topic = create_post.topic + trashed_topic.trash! + new_topic = create_post.topic + permalink = Permalink.create!(url: trashed_topic.relative_url, topic_id: new_topic.id) + + # no subfolder because router doesn't know about subfolder in this test + get "/t/#{trashed_topic.slug}/#{trashed_topic.id}" + expect(response.status).to eq(301) + expect(response).to redirect_to("/forum/t/#{new_topic.slug}/#{new_topic.id}") + + permalink.destroy + category = Fabricate(:category) + permalink = Permalink.create!(url: trashed_topic.relative_url, category_id: category.id) + get "/t/#{trashed_topic.slug}/#{trashed_topic.id}" + expect(response.status).to eq(301) + expect(response).to redirect_to("/forum/c/#{category.slug}") + + permalink.destroy + permalink = Permalink.create!(url: trashed_topic.relative_url, post_id: new_topic.posts.last.id) + get "/t/#{trashed_topic.slug}/#{trashed_topic.id}" + expect(response.status).to eq(301) + expect(response).to redirect_to("/forum/t/#{new_topic.slug}/#{new_topic.id}/#{new_topic.posts.last.post_number}") + end + it 'should return 404 and show Google search' do get "/t/nope-nope/99999999" expect(response.status).to eq(404)