diff --git a/lib/controllers/page_controller.rb b/lib/controllers/page_controller.rb index 03a1bbeb..4390ece9 100644 --- a/lib/controllers/page_controller.rb +++ b/lib/controllers/page_controller.rb @@ -215,6 +215,9 @@ def restrict_concealed(page) end get '/:slug' do + if params[:slug].include?(" ") + redirect "/#{params[:slug].tr(' ', '_')}", 301 + end @page = Page .select(:id, :slug, :title, :concealed, :revision, :updated_on, :compiled_content, :author_id) .eager_graph(:author) diff --git a/test/integration/app_not_logged_in_test.rb b/test/integration/app_not_logged_in_test.rb index a530dc55..1fdf5a39 100644 --- a/test/integration/app_not_logged_in_test.rb +++ b/test/integration/app_not_logged_in_test.rb @@ -88,6 +88,13 @@ def test_nonexistent_page assert_equal "/new/#{random_slug}", URI(redirect_location).path end + def test_page_with_space_in_slug_redirects_to_underscore + get "/foo%20bar" + + assert_equal 301, last_response.status + assert_equal "/foo_bar", URI(last_response["Location"]).path + end + def test_new get "/new"