diff --git a/app/controllers/permalinks_controller.rb b/app/controllers/permalinks_controller.rb index f78256da043..05deaf0d6a7 100644 --- a/app/controllers/permalinks_controller.rb +++ b/app/controllers/permalinks_controller.rb @@ -11,7 +11,7 @@ class PermalinksController < ApplicationController if permalink.external_url redirect_to permalink.external_url, status: :moved_permanently elsif permalink.target_url - redirect_to "#{Discourse::base_uri}#{permalink.target_url}", status: :moved_permanently + redirect_to permalink.target_url, status: :moved_permanently else raise Discourse::NotFound end diff --git a/app/models/permalink.rb b/app/models/permalink.rb index 086df294406..e7427d20bad 100644 --- a/app/models/permalink.rb +++ b/app/models/permalink.rb @@ -75,7 +75,7 @@ class Permalink < ActiveRecord::Base def target_url return external_url if external_url - return post.url if post + return "#{Discourse::base_uri}#{post.url}" if post return topic.relative_url if topic return category.url if category nil diff --git a/spec/controllers/permalinks_controller_spec.rb b/spec/controllers/permalinks_controller_spec.rb index d90b3de690c..aeca3b2cde5 100644 --- a/spec/controllers/permalinks_controller_spec.rb +++ b/spec/controllers/permalinks_controller_spec.rb @@ -10,6 +10,16 @@ describe PermalinksController do expect(response.status).to eq(301) end + it "should work for subfolder installs too" do + GlobalSetting.stubs(:relative_url_root).returns('/forum') + Discourse.stubs(:base_uri).returns("/forum") + permalink = Fabricate(:permalink) + Permalink.any_instance.stubs(:target_url).returns('/forum/t/the-topic-slug/42') + get :show, url: permalink.url + expect(response).to redirect_to('/forum/t/the-topic-slug/42') + expect(response.status).to eq(301) + end + it "should apply normalizations" do SiteSetting.permalink_normalizations = "/(.*)\\?.*/\\1"