diff --git a/config/application.rb b/config/application.rb index 266f043b..f0a53b14 100644 --- a/config/application.rb +++ b/config/application.rb @@ -47,5 +47,6 @@ module Locomotive config.filter_parameters << :password config.middleware.insert_after Rack::Lock, '::Locomotive::Middlewares::Fonts', :path => %r{^/fonts} + config.middleware.insert_after Rack::Lock, '::Locomotive::Middlewares::SeoTrailingSlash' end end diff --git a/config/initializers/dragonfly.rb b/config/initializers/dragonfly.rb index a54051a1..fdf2a685 100644 --- a/config/initializers/dragonfly.rb +++ b/config/initializers/dragonfly.rb @@ -33,7 +33,7 @@ unless Locomotive.engine? begin require 'rack/cache' Rails.application.middleware.insert_before 'Dragonfly::Middleware', 'Rack::Cache', { - :verbose => true, + :verbose => false, :metastore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/meta"), # URI encoded in case of spaces :entitystore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/body") } diff --git a/doc/TODO b/doc/TODO index 9adc6f04..3698ca84 100644 --- a/doc/TODO +++ b/doc/TODO @@ -21,7 +21,9 @@ x contents permalink (UI) x BUG: has_one / has_many. Delete an author x bushido changes in the master ? edit sidebar (inline editor). Unable to reset it -- SEO: support and support/ should be 2 different pages. Remove trailing slash +x SEO: support and support/ should be 2 different pages. Remove trailing slash +- Httparty (issue #...) +- Has_one => group by in the select BACKLOG: diff --git a/lib/locomotive/engine.rb b/lib/locomotive/engine.rb index b1f233cc..d06b588d 100644 --- a/lib/locomotive/engine.rb +++ b/lib/locomotive/engine.rb @@ -35,6 +35,7 @@ module Locomotive initializer "serving fonts" do |app| app.middleware.insert_after Rack::Lock, '::Locomotive::Middlewares::Fonts', :path => %r{^/fonts} + app.middleware.insert_after Rack::Lock, '::Locomotive::Middlewares::SeoTrailingSlash' end end diff --git a/lib/locomotive/middlewares.rb b/lib/locomotive/middlewares.rb index e241b9d3..e21c4d8f 100644 --- a/lib/locomotive/middlewares.rb +++ b/lib/locomotive/middlewares.rb @@ -1 +1,2 @@ -require 'locomotive/middlewares/fonts' \ No newline at end of file +require 'locomotive/middlewares/fonts' +require 'locomotive/middlewares/seo_trailing_slash' \ No newline at end of file diff --git a/lib/locomotive/middlewares/seo_trailing_slash.rb b/lib/locomotive/middlewares/seo_trailing_slash.rb new file mode 100644 index 00000000..6f5a1918 --- /dev/null +++ b/lib/locomotive/middlewares/seo_trailing_slash.rb @@ -0,0 +1,24 @@ +module Locomotive + module Middlewares + class SeoTrailingSlash + + def initialize(app, opts = {}) + @app = app + end + + def call(env) + path = env['PATH_INFO'] + + if !path.starts_with('/admin/') && (match = path.match(%r{(.+)/$})) + response = Rack::Response.new + response.redirect(match[1], 301) # moved permanently + response.finish + response.to_a + else + @app.call(env) + end + end + + end + end +end diff --git a/spec/lib/locomotive/render_spec.rb b/spec/lib/locomotive/render_spec.rb index 358cee92..4cbee81e 100644 --- a/spec/lib/locomotive/render_spec.rb +++ b/spec/lib/locomotive/render_spec.rb @@ -85,7 +85,7 @@ describe 'Locomotive rendering system' do @controller.send(:locomotive_page).should be_true end - context 'redirect page' do + context 'redirect' do before(:each) do @page.redirect = true diff --git a/spec/requests/seo_trailing_slash_spec.rb b/spec/requests/seo_trailing_slash_spec.rb new file mode 100644 index 00000000..a30641c1 --- /dev/null +++ b/spec/requests/seo_trailing_slash_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +describe Locomotive::Middlewares::SeoTrailingSlash do + + it 'does not process the "/" url' do + get '/' + response.status.should_not be(301) + end + + it 'does not process the "/admin/" url' do + get '/admin/' + response.status.should_not be(301) + end + + it 'does not process the "/admin/*" urls' do + get '/admin/login' + response.status.should_not be(301) + end + + it 'redirects to the url without the trailing slash' do + get '/hello_world/' + response.status.should be(301) + end + +end \ No newline at end of file